diff --git a/core-java-8/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java b/core-java-8/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java index 679e753c56..88fc175f38 100644 --- a/core-java-8/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java +++ b/core-java-8/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java @@ -5,14 +5,15 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import org.apache.commons.collections4.IterableUtils; import org.apache.commons.collections4.MapUtils; import com.google.common.collect.Maps; public class ConvertListToMapService { public Map convertListBeforeJava8(List list) { - Map map = new HashMap(); + + Map map = new HashMap<>(); + for (Animal animal : list) { map.put(animal.getId(), animal); } @@ -30,20 +31,9 @@ public class ConvertListToMapService { return map; } - public Map convertListWithApacheCommons1(List list) { + public Map convertListWithApacheCommons(List list) { - Map map = new HashMap(); - - IterableUtils.forEach(list, animal -> { - map.put(animal.getId(), animal); - }); - - return map; - } - - public Map convertListWithApacheCommons2(List list) { - - Map map = new HashMap(); + Map map = new HashMap<>(); MapUtils.populateMap(map, list, Animal::getId); diff --git a/core-java-8/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java b/core-java-8/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java index 4e78af08cd..a4234d2af4 100644 --- a/core-java-8/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java @@ -29,6 +29,7 @@ public class ConvertListToMapServiceUnitTest { list.add(cow); Animal goat = new Animal(5, "Goat"); list.add(goat); + } @Test @@ -56,18 +57,11 @@ public class ConvertListToMapServiceUnitTest { } @Test - public void givenAList_whenConvertWithApacheCommons1_thenReturnMapWithTheSameElements() { + public void givenAList_whenConvertWithApacheCommons_thenReturnMapWithTheSameElements() { - Map map = convertListService.convertListWithApacheCommons1(list); + Map map = convertListService.convertListWithApacheCommons(list); assertThat(map.values(), containsInAnyOrder(list.toArray())); } - @Test - public void givenAList_whenConvertWithApacheCommons2_thenReturnMapWithTheSameElements() { - - Map map = convertListService.convertListWithApacheCommons2(list); - - assertThat(map.values(), containsInAnyOrder(list.toArray())); - } } diff --git a/core-java-8/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDiplicatedIdToMapServiceUnitTest.java b/core-java-8/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDiplicatedIdToMapServiceUnitTest.java new file mode 100644 index 0000000000..6d3ad9e76e --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDiplicatedIdToMapServiceUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.convertlisttomap; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +public class ConvertListWithDiplicatedIdToMapServiceUnitTest { + List duplicatedIdList; + + private ConvertListToMapService convertListService = new ConvertListToMapService(); + + @Before + public void init() { + + this.duplicatedIdList = new ArrayList<>(); + + Animal cat = new Animal(1, "Cat"); + duplicatedIdList.add(cat); + Animal dog = new Animal(2, "Dog"); + duplicatedIdList.add(dog); + Animal pig = new Animal(3, "Pig"); + duplicatedIdList.add(pig); + Animal cow = new Animal(4, "Cow"); + duplicatedIdList.add(cow); + Animal goat = new Animal(4, "Goat"); + duplicatedIdList.add(goat); + + } + + @Test + public void givenADupIdList_whenConvertBeforeJava8_thenReturnMapWithRewrittenElement() { + + Map map = convertListService.convertListBeforeJava8(duplicatedIdList); + + assertThat(map.values(), hasSize(4)); + assertThat(map.values(), hasItem(duplicatedIdList.get(4))); + } + + @Test + public void givenADupIdList_whenConvertWithApacheCommons_thenReturnMapWithRewrittenElement() { + + Map map = convertListService.convertListWithApacheCommons(duplicatedIdList); + + assertThat(map.values(), hasSize(4)); + assertThat(map.values(), hasItem(duplicatedIdList.get(4))); + } + + @Test(expected = IllegalStateException.class) + public void givenADupIdList_whenConvertAfterJava8_thenException() { + + convertListService.convertListAfterJava8(duplicatedIdList); + } + + @Test(expected = IllegalArgumentException.class) + public void givenADupIdList_whenConvertWithGuava_thenException() { + + convertListService.convertListWithGuava(duplicatedIdList); + + } + +}