diff --git a/core-java-8/src/main/java/com/baeldung/convertlisttomap/Animal.java b/core-java-8/src/main/java/com/baeldung/convertlisttomap/Animal.java new file mode 100644 index 000000000000..b8eddf71a5de --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/convertlisttomap/Animal.java @@ -0,0 +1,27 @@ +package com.baeldung.convertlisttomap; + +public class Animal { + private int id; + private String name; + + public Animal(int id, String name) { + this.id = id; + this.setName(name); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} 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 new file mode 100644 index 000000000000..679e753c5642 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java @@ -0,0 +1,52 @@ +package com.baeldung.convertlisttomap; + +import java.util.HashMap; +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(); + for (Animal animal : list) { + map.put(animal.getId(), animal); + } + return map; + } + + public Map convertListAfterJava8(List list) { + Map map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal)); + return map; + } + + public Map convertListWithGuava(List list) { + + Map map = Maps.uniqueIndex(list, Animal::getId); + return map; + } + + public Map convertListWithApacheCommons1(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(); + + MapUtils.populateMap(map, list, Animal::getId); + + return map; + } +} 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 new file mode 100644 index 000000000000..4e78af08cd09 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java @@ -0,0 +1,73 @@ +package com.baeldung.convertlisttomap; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +public class ConvertListToMapServiceUnitTest { + List list; + + private ConvertListToMapService convertListService; + + @Before + public void init() { + this.convertListService = new ConvertListToMapService(); + this.list = new ArrayList<>(); + + Animal cat = new Animal(1, "Cat"); + list.add(cat); + Animal dog = new Animal(2, "Dog"); + list.add(dog); + Animal pig = new Animal(3, "Pig"); + list.add(pig); + Animal cow = new Animal(4, "Cow"); + list.add(cow); + Animal goat = new Animal(5, "Goat"); + list.add(goat); + } + + @Test + public void givenAList_whenConvertBeforeJava8_thenReturnMapWithTheSameElements() { + + Map map = convertListService.convertListBeforeJava8(list); + + assertThat(map.values(), containsInAnyOrder(list.toArray())); + } + + @Test + public void givenAList_whenConvertAfterJava8_thenReturnMapWithTheSameElements() { + + Map map = convertListService.convertListAfterJava8(list); + + assertThat(map.values(), containsInAnyOrder(list.toArray())); + } + + @Test + public void givenAList_whenConvertWithGuava_thenReturnMapWithTheSameElements() { + + Map map = convertListService.convertListWithGuava(list); + + assertThat(map.values(), containsInAnyOrder(list.toArray())); + } + + @Test + public void givenAList_whenConvertWithApacheCommons1_thenReturnMapWithTheSameElements() { + + Map map = convertListService.convertListWithApacheCommons1(list); + + assertThat(map.values(), containsInAnyOrder(list.toArray())); + } + + @Test + public void givenAList_whenConvertWithApacheCommons2_thenReturnMapWithTheSameElements() { + + Map map = convertListService.convertListWithApacheCommons2(list); + + assertThat(map.values(), containsInAnyOrder(list.toArray())); + } +}