Skip to content

Commit 770e8b0

Browse files
myluckagainpedja4
authored andcommitted
BAEL-2018 (eugenp#4753)
* BAEL-2018 * Update Animal.java * rename
1 parent a5b49fb commit 770e8b0

3 files changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.convertlisttomap;
2+
3+
public class Animal {
4+
private int id;
5+
private String name;
6+
7+
public Animal(int id, String name) {
8+
this.id = id;
9+
this.setName(name);
10+
}
11+
12+
public int getId() {
13+
return id;
14+
}
15+
16+
public void setId(int id) {
17+
this.id = id;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.convertlisttomap;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
8+
import org.apache.commons.collections4.IterableUtils;
9+
import org.apache.commons.collections4.MapUtils;
10+
import com.google.common.collect.Maps;
11+
12+
public class ConvertListToMapService {
13+
14+
public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
15+
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
16+
for (Animal animal : list) {
17+
map.put(animal.getId(), animal);
18+
}
19+
return map;
20+
}
21+
22+
public Map<Integer, Animal> convertListAfterJava8(List<Animal> list) {
23+
Map<Integer, Animal> map = list.stream().collect(Collectors.toMap(Animal::getId, animal -> animal));
24+
return map;
25+
}
26+
27+
public Map<Integer, Animal> convertListWithGuava(List<Animal> list) {
28+
29+
Map<Integer, Animal> map = Maps.uniqueIndex(list, Animal::getId);
30+
return map;
31+
}
32+
33+
public Map<Integer, Animal> convertListWithApacheCommons1(List<Animal> list) {
34+
35+
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
36+
37+
IterableUtils.forEach(list, animal -> {
38+
map.put(animal.getId(), animal);
39+
});
40+
41+
return map;
42+
}
43+
44+
public Map<Integer, Animal> convertListWithApacheCommons2(List<Animal> list) {
45+
46+
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
47+
48+
MapUtils.populateMap(map, list, Animal::getId);
49+
50+
return map;
51+
}
52+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.convertlisttomap;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.*;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
public class ConvertListToMapServiceUnitTest {
13+
List<Animal> list;
14+
15+
private ConvertListToMapService convertListService;
16+
17+
@Before
18+
public void init() {
19+
this.convertListService = new ConvertListToMapService();
20+
this.list = new ArrayList<>();
21+
22+
Animal cat = new Animal(1, "Cat");
23+
list.add(cat);
24+
Animal dog = new Animal(2, "Dog");
25+
list.add(dog);
26+
Animal pig = new Animal(3, "Pig");
27+
list.add(pig);
28+
Animal cow = new Animal(4, "Cow");
29+
list.add(cow);
30+
Animal goat = new Animal(5, "Goat");
31+
list.add(goat);
32+
}
33+
34+
@Test
35+
public void givenAList_whenConvertBeforeJava8_thenReturnMapWithTheSameElements() {
36+
37+
Map<Integer, Animal> map = convertListService.convertListBeforeJava8(list);
38+
39+
assertThat(map.values(), containsInAnyOrder(list.toArray()));
40+
}
41+
42+
@Test
43+
public void givenAList_whenConvertAfterJava8_thenReturnMapWithTheSameElements() {
44+
45+
Map<Integer, Animal> map = convertListService.convertListAfterJava8(list);
46+
47+
assertThat(map.values(), containsInAnyOrder(list.toArray()));
48+
}
49+
50+
@Test
51+
public void givenAList_whenConvertWithGuava_thenReturnMapWithTheSameElements() {
52+
53+
Map<Integer, Animal> map = convertListService.convertListWithGuava(list);
54+
55+
assertThat(map.values(), containsInAnyOrder(list.toArray()));
56+
}
57+
58+
@Test
59+
public void givenAList_whenConvertWithApacheCommons1_thenReturnMapWithTheSameElements() {
60+
61+
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons1(list);
62+
63+
assertThat(map.values(), containsInAnyOrder(list.toArray()));
64+
}
65+
66+
@Test
67+
public void givenAList_whenConvertWithApacheCommons2_thenReturnMapWithTheSameElements() {
68+
69+
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons2(list);
70+
71+
assertThat(map.values(), containsInAnyOrder(list.toArray()));
72+
}
73+
}

0 commit comments

Comments
 (0)