Merge pull request #7080 from rodrigolgraciano/BAEL-2936

BAEL-2936
This commit is contained in:
maibin 2019-06-04 05:44:40 +02:00 committed by GitHub
commit 8d5f6afde4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 4 deletions

View File

@ -15,8 +15,7 @@ public class ConvertToMap {
}
public Map<Integer, Book> listToMapWithDupKey(List<Book> 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<Integer, Book> listToConcurrentMap(List<Book> books) {

View File

@ -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<Integer, Book> booksByYear = convertToMap.listToMapWithDupKey(bookList);
assertEquals(2, booksByYear.size());
assertEquals("0395489318", booksByYear.get(1954).getIsbn());
}
@Test