Skip to content

Commit b15c4e0

Browse files
author
amitphaltankar
committed
aph-15360 java 21
1 parent 873fefc commit b15c4e0

File tree

62 files changed

+402
-528
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+402
-528
lines changed

.github/workflows/maven.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212

1313
steps:
1414
- uses: actions/checkout@v3
15-
- name: Set up JDK 17
15+
- name: Set up JDK 21
1616
uses: actions/setup-java@v3
1717
with:
18-
java-version: '17'
18+
java-version: '21'
1919
distribution: 'adopt'
2020
cache: 'maven'
2121
- name: Build with Maven

java-collections/collections-map/src/main/java/com/amitph/java/collections/map/MapInitializer.java

+20-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.collect.ImmutableMap;
44
import com.google.common.collect.Maps;
5-
65
import java.util.Collections;
76
import java.util.HashMap;
87
import java.util.Map;
@@ -21,13 +20,14 @@ public void initializeJavaHashMap() {
2120
}
2221

2322
public void initializeJavaHashMapUsingAnonymousSubClass() {
24-
Map<String, String> map = new HashMap<>() {
25-
{
26-
put("color", "black");
27-
put("drink", "coffee");
28-
put("shape", "slim");
29-
}
30-
};
23+
Map<String, String> map =
24+
new HashMap<>() {
25+
{
26+
put("color", "black");
27+
put("drink", "coffee");
28+
put("shape", "slim");
29+
}
30+
};
3131

3232
System.out.println(map);
3333
}
@@ -38,15 +38,13 @@ public void initializeImmutableJavaHashMap() {
3838
map.put("drink", "coffee");
3939
map.put("shape", "slim");
4040

41-
Map<String, String> immutableMap =
42-
Collections.unmodifiableMap(map);
41+
Map<String, String> immutableMap = Collections.unmodifiableMap(map);
4342

4443
System.out.println(immutableMap);
4544
}
4645

4746
public void initializeSingletonJavaHashMap() {
48-
Map<String, String> map =
49-
Collections.singletonMap("color", "black");
47+
Map<String, String> map = Collections.singletonMap("color", "black");
5048
System.out.println(map);
5149
}
5250

@@ -56,39 +54,37 @@ public void initializeEmptyJavaHashMap() {
5654
}
5755

5856
public void initializeImmutableJavaHashMapUsingGuava() {
59-
Map<String, String> immutableMap = ImmutableMap
60-
.of("color", "pink", "drink", "coffee", "shape", "slim");
57+
Map<String, String> immutableMap =
58+
ImmutableMap.of("color", "pink", "drink", "coffee", "shape", "slim");
6159

6260
System.out.println(immutableMap);
6361
}
6462

6563
public void initializeMutableJavaHashMapUsingGuava() {
66-
Map<String, String> immutableMap = ImmutableMap
67-
.of("color", "pink", "drink", "coffee", "shape", "slim");
64+
Map<String, String> immutableMap =
65+
ImmutableMap.of("color", "pink", "drink", "coffee", "shape", "slim");
6866

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

7371
public void initializeJavaHashMapUsingCollectorsToMap() {
7472
Set<String> set = Set.of("Pink", "Red", "Black");
75-
Map<String, String> map = set.stream()
76-
.collect(Collectors.toMap(String::toUpperCase, String::toLowerCase));
73+
Map<String, String> map =
74+
set.stream().collect(Collectors.toMap(String::toUpperCase, String::toLowerCase));
7775

7876
System.out.println(map);
7977
}
8078

8179
public void initializeJavaHashMapUsingMapOf() {
82-
Map<String, String> immutableMap =
83-
Map.of("color", "black", "drink", "coffee");
80+
Map<String, String> immutableMap = Map.of("color", "black", "drink", "coffee");
8481

8582
System.out.println(immutableMap);
8683
}
8784

8885
public void initializeJavaHashMapUsingMapOfEntries() {
89-
Map<String, String> immutableMap = Map.ofEntries(
90-
Map.entry("color", "pink"),
91-
Map.entry("drink", "coffee"));
86+
Map<String, String> immutableMap =
87+
Map.ofEntries(Map.entry("color", "pink"), Map.entry("drink", "coffee"));
9288

9389
System.out.println(immutableMap);
9490
}
@@ -126,4 +122,4 @@ public static void main(String[] a) {
126122
System.out.println("Initialize HashMap using factory method ofEntries()");
127123
initializer.initializeJavaHashMapUsingMapOfEntries();
128124
}
129-
}
125+
}

java-collections/collections-map/src/main/java/com/amitph/java/collections/map/merge/MergeMap.java

+23-27
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,29 @@
66
import java.util.stream.Stream;
77

88
public class MergeMap {
9-
private final Map<Integer, String> map1 = Map.of(
10-
1, "Ned",
11-
2, "Jon",
12-
3, "Khal"
13-
);
14-
15-
private final Map<Integer, String> map2 = Map.of(
16-
1, "Tywin",
17-
2, "Jon",
18-
4, "Petyr"
19-
);
20-
9+
private final Map<Integer, String> map1 =
10+
Map.of(
11+
1, "Ned",
12+
2, "Jon",
13+
3, "Khal");
14+
15+
private final Map<Integer, String> map2 =
16+
Map.of(
17+
1, "Tywin",
18+
2, "Jon",
19+
4, "Petyr");
2120

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

25-
Map<Integer, String> map3 = Stream.of(map1, map2)
26-
.flatMap(map -> map.entrySet().stream())
27-
.collect(Collectors.toMap(
28-
Map.Entry::getKey,
29-
Map.Entry::getValue,
30-
(value1, value2) -> value1));
24+
Map<Integer, String> map3 =
25+
Stream.of(map1, map2)
26+
.flatMap(map -> map.entrySet().stream())
27+
.collect(
28+
Collectors.toMap(
29+
Map.Entry::getKey,
30+
Map.Entry::getValue,
31+
(value1, value2) -> value1));
3132

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

3839
Map<Integer, String> map3 =
3940
Stream.concat(map1.entrySet().stream(), map2.entrySet().stream())
40-
.collect(Collectors.toMap(
41-
Map.Entry::getKey,
42-
Map.Entry::getValue,
43-
(v1, v2) -> v1));
41+
.collect(
42+
Collectors.toMap(
43+
Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
4444

4545
System.out.println(map3);
4646
}
@@ -58,11 +58,7 @@ private void usingMapMerge() {
5858
System.out.println("merge usingMapMerge");
5959

6060
Map<Integer, String> map3 = new HashMap<>(map1);
61-
map2.forEach((key, value) ->
62-
map3.merge(
63-
key,
64-
value,
65-
(value1, value2) -> value1));
61+
map2.forEach((key, value) -> map3.merge(key, value, (value1, value2) -> value1));
6662
System.out.println(map3);
6763
}
6864

java-collections/collections-map/src/main/java/com/amitph/java/collections/map/model/Product.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ public class Product implements Comparable<Product> {
1212
public int compareTo(Product product) {
1313
return name.compareTo(product.name);
1414
}
15-
}
15+
}

java-collections/collections-map/src/main/java/com/amitph/java/collections/map/sort/SortMap.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.amitph.java.collections.map.sort;
22

33
import com.amitph.java.collections.map.model.Product;
4-
54
import java.util.ArrayList;
65
import java.util.Collections;
76
import java.util.HashMap;
@@ -43,15 +42,11 @@ private void usingTreeMap() {
4342
private void usingJavaStreams() {
4443
System.out.println("Using JavaStreams: sort by key...");
4544

46-
map.entrySet().stream()
47-
.sorted(Map.Entry.comparingByKey())
48-
.forEach(System.out::println);
45+
map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(System.out::println);
4946

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

52-
map.entrySet().stream()
53-
.sorted(Map.Entry.comparingByValue())
54-
.forEach(System.out::println);
49+
map.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(System.out::println);
5550
}
5651

5752
private void usingTreeSet() {
@@ -89,4 +84,4 @@ public static void main(String[] a) {
8984
sortMap.usingTreeSet();
9085
sortMap.usingArrayList();
9186
}
92-
}
87+
}

java-collections/src/main/java/com/amitph/java/collections/ListSetConverter.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import com.google.common.collect.Lists;
44
import com.google.common.collect.Sets;
5-
import org.apache.commons.collections4.CollectionUtils;
6-
75
import java.util.ArrayList;
86
import java.util.HashSet;
97
import java.util.List;
108
import java.util.Set;
9+
import org.apache.commons.collections4.CollectionUtils;
1110

1211
public class ListSetConverter {
1312

@@ -69,7 +68,6 @@ public void listToSetWithApacheCommons() {
6968
System.out.println(set);
7069
}
7170

72-
7371
public static void main(String[] a) {
7472
ListSetConverter converter = new ListSetConverter();
7573

@@ -97,4 +95,4 @@ public static void main(String[] a) {
9795
System.out.println("List to Set using Apache Commons");
9896
converter.listToSetWithApacheCommons();
9997
}
100-
}
98+
}

java-collections/src/main/java/com/amitph/java/collections/MultiFieldSorter.java

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package com.amitph.java.collections;
22

33
import com.google.common.collect.ComparisonChain;
4+
import java.util.ArrayList;
5+
import java.util.Comparator;
6+
import java.util.List;
47
import lombok.Getter;
58
import lombok.RequiredArgsConstructor;
69
import lombok.ToString;
710
import org.apache.commons.lang3.builder.CompareToBuilder;
811

9-
import java.util.ArrayList;
10-
import java.util.Comparator;
11-
import java.util.List;
12-
1312
@RequiredArgsConstructor
1413
public class MultiFieldSorter {
15-
private final List<Student> collection = new ArrayList<>(List.of(
16-
new Student(1L, "Ray", 18),
17-
new Student(2L, "Bee", 18),
18-
new Student(3L, "Ray", 17),
19-
new Student(4L, "Bia", 15),
20-
new Student(5L, "Ria", 15)
21-
));
14+
private final List<Student> collection =
15+
new ArrayList<>(
16+
List.of(
17+
new Student(1L, "Ray", 18),
18+
new Student(2L, "Bee", 18),
19+
new Student(3L, "Ray", 17),
20+
new Student(4L, "Bia", 15),
21+
new Student(5L, "Ria", 15)));
2222

2323
public void sortUsingComparator_Field_by_Field() {
2424
collection.sort(new SimpleComparator());
@@ -36,9 +36,7 @@ public void sortUsingComparator_Commons_CompareToBuilder() {
3636
}
3737

3838
public void sortUsingComparator_Comparing() {
39-
collection.sort(Comparator
40-
.comparing(Student::getName)
41-
.thenComparing(Student::getAge));
39+
collection.sort(Comparator.comparing(Student::getName).thenComparing(Student::getAge));
4240
collection.forEach(System.out::println);
4341
}
4442

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

79-
return (difference != 0) ? difference
80-
: Integer.compare(o1.getAge(), o2.getAge());
77+
return (difference != 0) ? difference : Integer.compare(o1.getAge(), o2.getAge());
8178
}
8279
}
8380

@@ -99,4 +96,4 @@ public int compare(Student o1, Student o2) {
9996
.append(o1.getAge(), o2.getAge())
10097
.build();
10198
}
102-
}
99+
}

java-collections/src/main/java/com/amitph/java/collections/list/ListAndArrayConverter.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import com.google.common.collect.Lists;
44
import com.google.common.primitives.Ints;
5-
import org.apache.commons.collections4.CollectionUtils;
6-
75
import java.util.ArrayList;
86
import java.util.Arrays;
97
import java.util.List;
8+
import org.apache.commons.collections4.CollectionUtils;
109

1110
public class ListAndArrayConverter {
1211
public void arrayToListUsingJava_createImmutableList() {
13-
Integer[] array = new Integer[]{98, 99, 100};
12+
Integer[] array = new Integer[] {98, 99, 100};
1413
List<Integer> list = Arrays.asList(array);
1514

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

2625
public void arrayToListUsingJava_createMutableList() {
27-
Integer[] array = new Integer[]{98, 99, 100};
28-
List<Integer> list =
29-
new ArrayList<>(Arrays.asList(array));
26+
Integer[] array = new Integer[] {98, 99, 100};
27+
List<Integer> list = new ArrayList<>(Arrays.asList(array));
3028

3129
System.out.println(list);
3230
}
3331

3432
public void arrayToListUsingApacheCommons() {
35-
Integer[] array = new Integer[]{98, 99, 100};
33+
Integer[] array = new Integer[] {98, 99, 100};
3634
List<Integer> list = new ArrayList<>();
3735
CollectionUtils.addAll(list, array);
3836

3937
System.out.println(list);
4038
}
4139

4240
public void arrayToListUsingGuava() {
43-
Integer[] array = new Integer[]{98, 99, 100};
41+
Integer[] array = new Integer[] {98, 99, 100};
4442
List<Integer> list = Lists.newArrayList(array);
4543

4644
System.out.println(list);
@@ -74,4 +72,4 @@ public static void main(String[] a) {
7472
System.out.println("List to Array using Guava");
7573
converter.listToArrayUsingGuava();
7674
}
77-
}
75+
}

0 commit comments

Comments
 (0)