diff --git a/java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java b/java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java index 3c14dfdba6..e33d9ee212 100644 --- a/java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java +++ b/java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java @@ -15,8 +15,7 @@ public class ConvertToMap { } public Map listToMapWithDupKey(List books) { - return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), - (o1, o2) -> o1)); + return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), (existing, replacement) -> existing)); } public Map listToConcurrentMap(List books) { diff --git a/java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java b/java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java index d11221bbf7..d6eab461d7 100644 --- a/java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java +++ b/java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java @@ -2,6 +2,7 @@ package com.baeldung.convertToMap; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import static org.junit.Assert.*; @@ -34,8 +35,10 @@ public class ConvertToMapUnitTest { } @Test - public void whenMapHasDuplicateKey_with_merge_function() { - assertTrue(convertToMap.listToMapWithDupKey(bookList).size() == 2); + public void whenMapHasDuplicateKeyThenMergeFunctionHandlesCollision() { + Map booksByYear = convertToMap.listToMapWithDupKey(bookList); + assertEquals(2, booksByYear.size()); + assertEquals("0395489318", booksByYear.get(1954).getIsbn()); } @Test