From 1c61ba62abf38bd6c3962dc2a4a293baf4523a18 Mon Sep 17 00:00:00 2001 From: joe zhang Date: Fri, 19 Jun 2020 11:06:53 +0800 Subject: [PATCH 1/4] Leasy Zhang/shiwangzhihe@gmail.com --- .../convertlisttomap/ListToMapUnitTest.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java new file mode 100644 index 0000000000..e2340a5a9a --- /dev/null +++ b/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java @@ -0,0 +1,102 @@ +package com.baeldung.convertlisttomap; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +import org.junit.Test; + +public class ListToMapUnitTest { + + @Test + public void givenAList_whenConvertWithJava8GroupBy_thenReturnMap() { + List strings = Arrays.asList("List", "Map", "Set", "Tree"); + + Map> convertedMap = new HashMap<>(); + + Supplier> listSupplier = () -> { + return new ArrayList<>(); + }; + + Supplier>> mapFactory = () -> { + return new HashMap<>(); + }; + convertedMap = strings.stream() + .collect(Collectors.groupingBy(String::length, mapFactory, Collectors.toCollection(listSupplier))); + + assertEquals(2, convertedMap.size()); + assertTrue(convertedMap.get(3) + .contains("Map")); + } + + @Test + public void givenAList_whenConvertWithJava8Collect_thenReturnMap() { + List strings = Arrays.asList("List", "Map", "Set", "Tree"); + + Map> convertedMap = new HashMap<>(); + + Supplier>> mapFactory = () -> { + return new HashMap<>(); + }; + + Supplier> listSupplier = () -> { + return new ArrayList<>(); + }; + + BiConsumer>, String> accumulator = (response, element) -> { + Integer key = element.length(); + List values = response.getOrDefault(key, listSupplier.get()); + values.add(element); + response.put(key, values); + }; + + BiConsumer>, Map>> combiner = (res1, res2) -> { + res1.putAll(res2); + }; + + convertedMap = strings.stream() + .collect(mapFactory, accumulator, combiner); + + assertEquals(2, convertedMap.size()); + assertTrue(convertedMap.get(3) + .contains("Map")); + } + + @Test + public void givenAList_whenConvertWithCollectorToMap_thenReturnMap() { + List strings = Arrays.asList("List", "Map", "Set", "Tree"); + + Map> convertedMap = new HashMap<>(); + + Supplier>> mapFactory = () -> { + return new HashMap<>(); + }; + + Supplier> listSupplier = () -> { + return new ArrayList<>(); + }; + + convertedMap = strings.stream() + .collect(Collectors.toMap(String::length, (p) -> { + List strs = listSupplier.get(); + strs.add(p); + return strs; + }, (existing, replacement) -> { + existing.addAll(replacement); + return existing; + }, mapFactory)); + + assertEquals(2, convertedMap.size()); + assertTrue(convertedMap.get(3) + .contains("Map")); + } + +} From 672ffdbe9f9fe6de186e38a4a7d28aa6e95378ee Mon Sep 17 00:00:00 2001 From: joe zhang Date: Mon, 29 Jun 2020 22:34:19 +0800 Subject: [PATCH 2/4] update unit test for collectors.toMap method --- .../convertlisttomap/ListToMapUnitTest.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java index e2340a5a9a..5a875a904e 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java @@ -9,6 +9,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.BiConsumer; +import java.util.function.BinaryOperator; +import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -84,15 +86,23 @@ public class ListToMapUnitTest { return new ArrayList<>(); }; + Function keyMapper = (element) -> { + return element.length(); + }; + + Function> valueMapper = (element) -> { + List collection = listSupplier.get(); + collection.add(element); + return collection; + }; + + BinaryOperator> mergeFunction = (existing, replacement) -> { + existing.addAll(replacement); + return existing; + }; + convertedMap = strings.stream() - .collect(Collectors.toMap(String::length, (p) -> { - List strs = listSupplier.get(); - strs.add(p); - return strs; - }, (existing, replacement) -> { - existing.addAll(replacement); - return existing; - }, mapFactory)); + .collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapFactory)); assertEquals(2, convertedMap.size()); assertTrue(convertedMap.get(3) From 4f987c9198d52a4068454f7f9d3e61266e1ac08e Mon Sep 17 00:00:00 2001 From: joe zhang Date: Tue, 7 Jul 2020 13:20:06 +0800 Subject: [PATCH 3/4] update labmda function --- .../convertlisttomap/ListToMapUnitTest.java | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java index 5a875a904e..4ca5671e29 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java @@ -24,13 +24,9 @@ public class ListToMapUnitTest { Map> convertedMap = new HashMap<>(); - Supplier> listSupplier = () -> { - return new ArrayList<>(); - }; + Supplier> listSupplier = ArrayList::new; - Supplier>> mapFactory = () -> { - return new HashMap<>(); - }; + Supplier>> mapFactory = HashMap::new; convertedMap = strings.stream() .collect(Collectors.groupingBy(String::length, mapFactory, Collectors.toCollection(listSupplier))); @@ -45,13 +41,9 @@ public class ListToMapUnitTest { Map> convertedMap = new HashMap<>(); - Supplier>> mapFactory = () -> { - return new HashMap<>(); - }; + Supplier>> mapFactory = HashMap::new; - Supplier> listSupplier = () -> { - return new ArrayList<>(); - }; + Supplier> listSupplier = ArrayList::new; BiConsumer>, String> accumulator = (response, element) -> { Integer key = element.length(); @@ -78,17 +70,11 @@ public class ListToMapUnitTest { Map> convertedMap = new HashMap<>(); - Supplier>> mapFactory = () -> { - return new HashMap<>(); - }; + Supplier>> mapFactory = HashMap::new; - Supplier> listSupplier = () -> { - return new ArrayList<>(); - }; + Supplier> listSupplier = ArrayList::new; - Function keyMapper = (element) -> { - return element.length(); - }; + Function keyMapper = String::length; Function> valueMapper = (element) -> { List collection = listSupplier.get(); From 7cbb0833d40610344c1d11da0cf7597ca24e52ad Mon Sep 17 00:00:00 2001 From: joe zhang Date: Sun, 12 Jul 2020 16:19:08 +0800 Subject: [PATCH 4/4] refactor unit test into utility class --- .../convertlisttomap/ListToMapConverter.java | 70 +++++++++++++++++ .../convertlisttomap/ListToMapUnitTest.java | 78 ++++--------------- 2 files changed, 83 insertions(+), 65 deletions(-) create mode 100644 java-collections-conversions-2/src/main/java/com/baeldung/convertlisttomap/ListToMapConverter.java diff --git a/java-collections-conversions-2/src/main/java/com/baeldung/convertlisttomap/ListToMapConverter.java b/java-collections-conversions-2/src/main/java/com/baeldung/convertlisttomap/ListToMapConverter.java new file mode 100644 index 0000000000..8450f54f9d --- /dev/null +++ b/java-collections-conversions-2/src/main/java/com/baeldung/convertlisttomap/ListToMapConverter.java @@ -0,0 +1,70 @@ +package com.baeldung.convertlisttomap; + +import java.util.List; +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +/** + * Convert a string list to a map whose key is the string's length and value is the collection with same length. + * Give a list {"Baeldung", "is", "very", "cool"}. + * After conversion we'll get a map like: + * {8 : ["Baeldung"], 2 : ["is"], 4 : ["very", "cool"]}. + * + * @author leasy.zhang + * + */ +public class ListToMapConverter { + + public Map> groupingByStringLength(List source, + Supplier>> mapSupplier, + Supplier> listSupplier) { + + return source.stream() + .collect(Collectors.groupingBy(String::length, mapSupplier, Collectors.toCollection(listSupplier))); + } + + public Map> streamCollectByStringLength(List source, + Supplier>> mapSupplier, + Supplier> listSupplier) { + + BiConsumer>, String> accumulator = (response, element) -> { + Integer key = element.length(); + List values = response.getOrDefault(key, listSupplier.get()); + values.add(element); + response.put(key, values); + }; + + BiConsumer>, Map>> combiner = (res1, res2) -> { + res1.putAll(res2); + }; + + return source.stream() + .collect(mapSupplier, accumulator, combiner); + } + + public Map> collectorToMapByStringLength(List source, + Supplier>> mapSupplier, + Supplier> listSupplier) { + + Function keyMapper = String::length; + + Function> valueMapper = (element) -> { + List collection = listSupplier.get(); + collection.add(element); + return collection; + }; + + BinaryOperator> mergeFunction = (existing, replacement) -> { + existing.addAll(replacement); + return existing; + }; + + return source.stream() + .collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier)); + } + +} diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java index 4ca5671e29..2b43813822 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java @@ -1,6 +1,5 @@ package com.baeldung.convertlisttomap; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.ArrayList; @@ -8,89 +7,38 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.BiConsumer; -import java.util.function.BinaryOperator; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.stream.Collectors; +import org.junit.Before; import org.junit.Test; public class ListToMapUnitTest { + private ListToMapConverter converter; + private List source; + + @Before + public void setUp() { + converter = new ListToMapConverter(); + source = Arrays.asList("List", "Map", "Set", "Tree"); + } + @Test public void givenAList_whenConvertWithJava8GroupBy_thenReturnMap() { - List strings = Arrays.asList("List", "Map", "Set", "Tree"); - - Map> convertedMap = new HashMap<>(); - - Supplier> listSupplier = ArrayList::new; - - Supplier>> mapFactory = HashMap::new; - convertedMap = strings.stream() - .collect(Collectors.groupingBy(String::length, mapFactory, Collectors.toCollection(listSupplier))); - - assertEquals(2, convertedMap.size()); + Map> convertedMap = converter.groupingByStringLength(source, HashMap::new, ArrayList::new); assertTrue(convertedMap.get(3) .contains("Map")); } @Test public void givenAList_whenConvertWithJava8Collect_thenReturnMap() { - List strings = Arrays.asList("List", "Map", "Set", "Tree"); - - Map> convertedMap = new HashMap<>(); - - Supplier>> mapFactory = HashMap::new; - - Supplier> listSupplier = ArrayList::new; - - BiConsumer>, String> accumulator = (response, element) -> { - Integer key = element.length(); - List values = response.getOrDefault(key, listSupplier.get()); - values.add(element); - response.put(key, values); - }; - - BiConsumer>, Map>> combiner = (res1, res2) -> { - res1.putAll(res2); - }; - - convertedMap = strings.stream() - .collect(mapFactory, accumulator, combiner); - - assertEquals(2, convertedMap.size()); + Map> convertedMap = converter.streamCollectByStringLength(source, HashMap::new, ArrayList::new); assertTrue(convertedMap.get(3) .contains("Map")); } @Test public void givenAList_whenConvertWithCollectorToMap_thenReturnMap() { - List strings = Arrays.asList("List", "Map", "Set", "Tree"); - - Map> convertedMap = new HashMap<>(); - - Supplier>> mapFactory = HashMap::new; - - Supplier> listSupplier = ArrayList::new; - - Function keyMapper = String::length; - - Function> valueMapper = (element) -> { - List collection = listSupplier.get(); - collection.add(element); - return collection; - }; - - BinaryOperator> mergeFunction = (existing, replacement) -> { - existing.addAll(replacement); - return existing; - }; - - convertedMap = strings.stream() - .collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapFactory)); - - assertEquals(2, convertedMap.size()); + Map> convertedMap = converter.collectorToMapByStringLength(source, HashMap::new, ArrayList::new); assertTrue(convertedMap.get(3) .contains("Map")); }