BAEL-2936
This commit is contained in:
parent
e4e4a2bfa7
commit
3cea8b6df1
|
@ -15,8 +15,7 @@ public class ConvertToMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<Integer, Book> listToMapWithDupKey(List<Book> books) {
|
public Map<Integer, Book> listToMapWithDupKey(List<Book> books) {
|
||||||
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(),
|
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), (existing, replacement) -> existing));
|
||||||
(o1, o2) -> o1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<Integer, Book> listToConcurrentMap(List<Book> books) {
|
public Map<Integer, Book> listToConcurrentMap(List<Book> books) {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.baeldung.convertToMap;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
@ -34,8 +35,10 @@ public class ConvertToMapUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenMapHasDuplicateKey_with_merge_function() {
|
public void whenMapHasDuplicateKeyThenMergeFunctionHandlesCollision() {
|
||||||
assertTrue(convertToMap.listToMapWithDupKey(bookList).size() == 2);
|
Map<Integer, Book> booksByYear = convertToMap.listToMapWithDupKey(bookList);
|
||||||
|
assertEquals(2, booksByYear.size());
|
||||||
|
assertEquals("0395489318", booksByYear.get(1954).getIsbn());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue