update unit test for collectors.toMap method

This commit is contained in:
joe zhang 2020-06-29 22:34:19 +08:00
parent 1c61ba62ab
commit 672ffdbe9f

View File

@ -9,6 +9,8 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -84,15 +86,23 @@ public class ListToMapUnitTest {
return new ArrayList<>(); return new ArrayList<>();
}; };
convertedMap = strings.stream() Function<String, Integer> keyMapper = (element) -> {
.collect(Collectors.toMap(String::length, (p) -> { return element.length();
List<String> strs = listSupplier.get(); };
strs.add(p);
return strs; Function<String, List<String>> valueMapper = (element) -> {
}, (existing, replacement) -> { List<String> collection = listSupplier.get();
collection.add(element);
return collection;
};
BinaryOperator<List<String>> mergeFunction = (existing, replacement) -> {
existing.addAll(replacement); existing.addAll(replacement);
return existing; return existing;
}, mapFactory)); };
convertedMap = strings.stream()
.collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapFactory));
assertEquals(2, convertedMap.size()); assertEquals(2, convertedMap.size());
assertTrue(convertedMap.get(3) assertTrue(convertedMap.get(3)