Skip to content

aph-15360 java 21 #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '17'
java-version: '21'
distribution: 'adopt'
cache: 'maven'
- name: Build with Maven
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -21,13 +20,14 @@ public void initializeJavaHashMap() {
}

public void initializeJavaHashMapUsingAnonymousSubClass() {
Map<String, String> map = new HashMap<>() {
{
put("color", "black");
put("drink", "coffee");
put("shape", "slim");
}
};
Map<String, String> map =
new HashMap<>() {
{
put("color", "black");
put("drink", "coffee");
put("shape", "slim");
}
};

System.out.println(map);
}
Expand All @@ -38,15 +38,13 @@ public void initializeImmutableJavaHashMap() {
map.put("drink", "coffee");
map.put("shape", "slim");

Map<String, String> immutableMap =
Collections.unmodifiableMap(map);
Map<String, String> immutableMap = Collections.unmodifiableMap(map);

System.out.println(immutableMap);
}

public void initializeSingletonJavaHashMap() {
Map<String, String> map =
Collections.singletonMap("color", "black");
Map<String, String> map = Collections.singletonMap("color", "black");
System.out.println(map);
}

Expand All @@ -56,39 +54,37 @@ public void initializeEmptyJavaHashMap() {
}

public void initializeImmutableJavaHashMapUsingGuava() {
Map<String, String> immutableMap = ImmutableMap
.of("color", "pink", "drink", "coffee", "shape", "slim");
Map<String, String> immutableMap =
ImmutableMap.of("color", "pink", "drink", "coffee", "shape", "slim");

System.out.println(immutableMap);
}

public void initializeMutableJavaHashMapUsingGuava() {
Map<String, String> immutableMap = ImmutableMap
.of("color", "pink", "drink", "coffee", "shape", "slim");
Map<String, String> immutableMap =
ImmutableMap.of("color", "pink", "drink", "coffee", "shape", "slim");

Map<String, String> mutableMap = Maps.newHashMap(immutableMap);
System.out.println(mutableMap);
}

public void initializeJavaHashMapUsingCollectorsToMap() {
Set<String> set = Set.of("Pink", "Red", "Black");
Map<String, String> map = set.stream()
.collect(Collectors.toMap(String::toUpperCase, String::toLowerCase));
Map<String, String> map =
set.stream().collect(Collectors.toMap(String::toUpperCase, String::toLowerCase));

System.out.println(map);
}

public void initializeJavaHashMapUsingMapOf() {
Map<String, String> immutableMap =
Map.of("color", "black", "drink", "coffee");
Map<String, String> immutableMap = Map.of("color", "black", "drink", "coffee");

System.out.println(immutableMap);
}

public void initializeJavaHashMapUsingMapOfEntries() {
Map<String, String> immutableMap = Map.ofEntries(
Map.entry("color", "pink"),
Map.entry("drink", "coffee"));
Map<String, String> immutableMap =
Map.ofEntries(Map.entry("color", "pink"), Map.entry("drink", "coffee"));

System.out.println(immutableMap);
}
Expand Down Expand Up @@ -126,4 +122,4 @@ public static void main(String[] a) {
System.out.println("Initialize HashMap using factory method ofEntries()");
initializer.initializeJavaHashMapUsingMapOfEntries();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@
import java.util.stream.Stream;

public class MergeMap {
private final Map<Integer, String> map1 = Map.of(
1, "Ned",
2, "Jon",
3, "Khal"
);

private final Map<Integer, String> map2 = Map.of(
1, "Tywin",
2, "Jon",
4, "Petyr"
);

private final Map<Integer, String> map1 =
Map.of(
1, "Ned",
2, "Jon",
3, "Khal");

private final Map<Integer, String> map2 =
Map.of(
1, "Tywin",
2, "Jon",
4, "Petyr");

private void usingStreamOf() {
System.out.println("merge usingStreamOf");

Map<Integer, String> map3 = Stream.of(map1, map2)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(value1, value2) -> value1));
Map<Integer, String> map3 =
Stream.of(map1, map2)
.flatMap(map -> map.entrySet().stream())
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(value1, value2) -> value1));

System.out.println(map3);
}
Expand All @@ -37,10 +38,9 @@ private void usingStreamConcat() {

Map<Integer, String> map3 =
Stream.concat(map1.entrySet().stream(), map2.entrySet().stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(v1, v2) -> v1));
.collect(
Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));

System.out.println(map3);
}
Expand All @@ -58,11 +58,7 @@ private void usingMapMerge() {
System.out.println("merge usingMapMerge");

Map<Integer, String> map3 = new HashMap<>(map1);
map2.forEach((key, value) ->
map3.merge(
key,
value,
(value1, value2) -> value1));
map2.forEach((key, value) -> map3.merge(key, value, (value1, value2) -> value1));
System.out.println(map3);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public class Product implements Comparable<Product> {
public int compareTo(Product product) {
return name.compareTo(product.name);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.amitph.java.collections.map.sort;

import com.amitph.java.collections.map.model.Product;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -43,15 +42,11 @@ private void usingTreeMap() {
private void usingJavaStreams() {
System.out.println("Using JavaStreams: sort by key...");

map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(System.out::println);
map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(System.out::println);

System.out.println("Using JavaStreams: sort by value...");

map.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.forEach(System.out::println);
map.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(System.out::println);
}

private void usingTreeSet() {
Expand Down Expand Up @@ -89,4 +84,4 @@ public static void main(String[] a) {
sortMap.usingTreeSet();
sortMap.usingArrayList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;

public class ListSetConverter {

Expand Down Expand Up @@ -69,7 +68,6 @@ public void listToSetWithApacheCommons() {
System.out.println(set);
}


public static void main(String[] a) {
ListSetConverter converter = new ListSetConverter();

Expand Down Expand Up @@ -97,4 +95,4 @@ public static void main(String[] a) {
System.out.println("List to Set using Apache Commons");
converter.listToSetWithApacheCommons();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.amitph.java.collections;

import com.google.common.collect.ComparisonChain;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.builder.CompareToBuilder;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@RequiredArgsConstructor
public class MultiFieldSorter {
private final List<Student> collection = new ArrayList<>(List.of(
new Student(1L, "Ray", 18),
new Student(2L, "Bee", 18),
new Student(3L, "Ray", 17),
new Student(4L, "Bia", 15),
new Student(5L, "Ria", 15)
));
private final List<Student> collection =
new ArrayList<>(
List.of(
new Student(1L, "Ray", 18),
new Student(2L, "Bee", 18),
new Student(3L, "Ray", 17),
new Student(4L, "Bia", 15),
new Student(5L, "Ria", 15)));

public void sortUsingComparator_Field_by_Field() {
collection.sort(new SimpleComparator());
Expand All @@ -36,9 +36,7 @@ public void sortUsingComparator_Commons_CompareToBuilder() {
}

public void sortUsingComparator_Comparing() {
collection.sort(Comparator
.comparing(Student::getName)
.thenComparing(Student::getAge));
collection.sort(Comparator.comparing(Student::getName).thenComparing(Student::getAge));
collection.forEach(System.out::println);
}

Expand Down Expand Up @@ -76,8 +74,7 @@ class SimpleComparator implements Comparator<Student> {
public int compare(Student o1, Student o2) {
int difference = o1.getName().compareTo(o2.getName());

return (difference != 0) ? difference
: Integer.compare(o1.getAge(), o2.getAge());
return (difference != 0) ? difference : Integer.compare(o1.getAge(), o2.getAge());
}
}

Expand All @@ -99,4 +96,4 @@ public int compare(Student o1, Student o2) {
.append(o1.getAge(), o2.getAge())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;

public class ListAndArrayConverter {
public void arrayToListUsingJava_createImmutableList() {
Integer[] array = new Integer[]{98, 99, 100};
Integer[] array = new Integer[] {98, 99, 100};
List<Integer> list = Arrays.asList(array);

System.out.println(list);
Expand All @@ -24,23 +23,22 @@ public void listToArrayUsingJava() {
}

public void arrayToListUsingJava_createMutableList() {
Integer[] array = new Integer[]{98, 99, 100};
List<Integer> list =
new ArrayList<>(Arrays.asList(array));
Integer[] array = new Integer[] {98, 99, 100};
List<Integer> list = new ArrayList<>(Arrays.asList(array));

System.out.println(list);
}

public void arrayToListUsingApacheCommons() {
Integer[] array = new Integer[]{98, 99, 100};
Integer[] array = new Integer[] {98, 99, 100};
List<Integer> list = new ArrayList<>();
CollectionUtils.addAll(list, array);

System.out.println(list);
}

public void arrayToListUsingGuava() {
Integer[] array = new Integer[]{98, 99, 100};
Integer[] array = new Integer[] {98, 99, 100};
List<Integer> list = Lists.newArrayList(array);

System.out.println(list);
Expand Down Expand Up @@ -74,4 +72,4 @@ public static void main(String[] a) {
System.out.println("List to Array using Guava");
converter.listToArrayUsingGuava();
}
}
}
Loading
Loading