Skip to content

Commit de057dc

Browse files
author
joe zhang
committed
update labmda function
1 parent 4ea2478 commit de057dc

1 file changed

Lines changed: 7 additions & 21 deletions

File tree

java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ public void givenAList_whenConvertWithJava8GroupBy_thenReturnMap() {
2424

2525
Map<Integer, List<String>> convertedMap = new HashMap<>();
2626

27-
Supplier<List<String>> listSupplier = () -> {
28-
return new ArrayList<>();
29-
};
27+
Supplier<List<String>> listSupplier = ArrayList::new;
3028

31-
Supplier<Map<Integer, List<String>>> mapFactory = () -> {
32-
return new HashMap<>();
33-
};
29+
Supplier<Map<Integer, List<String>>> mapFactory = HashMap::new;
3430
convertedMap = strings.stream()
3531
.collect(Collectors.groupingBy(String::length, mapFactory, Collectors.toCollection(listSupplier)));
3632

@@ -45,13 +41,9 @@ public void givenAList_whenConvertWithJava8Collect_thenReturnMap() {
4541

4642
Map<Integer, List<String>> convertedMap = new HashMap<>();
4743

48-
Supplier<Map<Integer, List<String>>> mapFactory = () -> {
49-
return new HashMap<>();
50-
};
44+
Supplier<Map<Integer, List<String>>> mapFactory = HashMap::new;
5145

52-
Supplier<List<String>> listSupplier = () -> {
53-
return new ArrayList<>();
54-
};
46+
Supplier<List<String>> listSupplier = ArrayList::new;
5547

5648
BiConsumer<Map<Integer, List<String>>, String> accumulator = (response, element) -> {
5749
Integer key = element.length();
@@ -78,17 +70,11 @@ public void givenAList_whenConvertWithCollectorToMap_thenReturnMap() {
7870

7971
Map<Integer, List<String>> convertedMap = new HashMap<>();
8072

81-
Supplier<Map<Integer, List<String>>> mapFactory = () -> {
82-
return new HashMap<>();
83-
};
73+
Supplier<Map<Integer, List<String>>> mapFactory = HashMap::new;
8474

85-
Supplier<List<String>> listSupplier = () -> {
86-
return new ArrayList<>();
87-
};
75+
Supplier<List<String>> listSupplier = ArrayList::new;
8876

89-
Function<String, Integer> keyMapper = (element) -> {
90-
return element.length();
91-
};
77+
Function<String, Integer> keyMapper = String::length;
9278

9379
Function<String, List<String>> valueMapper = (element) -> {
9480
List<String> collection = listSupplier.get();

0 commit comments

Comments
 (0)