Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
for (Animal animal : list) {
map.put(animal.getId(), animal);
}
return map;
}

public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) {
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal));
return map;
}

public Map<Integer, Animal> convertListWithGuava(List<Animal> list) {

Map<Integer, Animal> map = Maps.uniqueIndex(list, Animal::getId);
return map;
}

public Map<Integer, Animal> convertListWithApacheCommons1(List<Animal> list) {

Map<Integer, Animal> map = new HashMap<Integer, Animal>();

IterableUtils.forEach(list, animal -> {
map.put(animal.getId(), animal);
});

return map;
}

public Map<Integer, Animal> convertListWithApacheCommons2(List<Animal> list) {

Map<Integer, Animal> map = new HashMap<Integer, Animal>();

MapUtils.populateMap(map, list, Animal::getId);

return map;
}
}
Original file line number Diff line number Diff line change
@@ -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<Animal> 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<Integer, Animal> map = convertListService.convertListBeforeJava8(list);

assertThat(map.values(), containsInAnyOrder(list.toArray()));
}

@Test
public void givenAList_whenConvertAfterJava8_thenReturnMapWithTheSameElements() {

Map<Integer, Animal> map = convertListService.convertListAfterJava8(list);

assertThat(map.values(), containsInAnyOrder(list.toArray()));
}

@Test
public void givenAList_whenConvertWithGuava_thenReturnMapWithTheSameElements() {

Map<Integer, Animal> map = convertListService.convertListWithGuava(list);

assertThat(map.values(), containsInAnyOrder(list.toArray()));
}

@Test
public void givenAList_whenConvertWithApacheCommons1_thenReturnMapWithTheSameElements() {

Map<Integer, Animal> map = convertListService.convertListWithApacheCommons1(list);

assertThat(map.values(), containsInAnyOrder(list.toArray()));
}

@Test
public void givenAList_whenConvertWithApacheCommons2_thenReturnMapWithTheSameElements() {

Map<Integer, Animal> map = convertListService.convertListWithApacheCommons2(list);

assertThat(map.values(), containsInAnyOrder(list.toArray()));
}
}