Skip to content

Commit 8f52c3f

Browse files
myluckagainpedja4
authored andcommitted
Bael 2018 (eugenp#4774)
* BAEL-2018 * Update Animal.java * rename * tests added * generic type shorten
1 parent b501487 commit 8f52c3f

3 files changed

Lines changed: 76 additions & 24 deletions

File tree

‎core-java-8/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
import java.util.Map;
66
import java.util.stream.Collectors;
77

8-
import org.apache.commons.collections4.IterableUtils;
98
import org.apache.commons.collections4.MapUtils;
109
import com.google.common.collect.Maps;
1110

1211
public class ConvertListToMapService {
1312

1413
public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
15-
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
14+
15+
Map<Integer, Animal> map = new HashMap<>();
16+
1617
for (Animal animal : list) {
1718
map.put(animal.getId(), animal);
1819
}
@@ -30,20 +31,9 @@ public Map<Integer, Animal> convertListWithGuava(List<Animal> list) {
3031
return map;
3132
}
3233

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) {
34+
public Map<Integer, Animal> convertListWithApacheCommons(List<Animal> list) {
4535

46-
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
36+
Map<Integer, Animal> map = new HashMap<>();
4737

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

‎core-java-8/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void init() {
2929
list.add(cow);
3030
Animal goat = new Animal(5, "Goat");
3131
list.add(goat);
32+
3233
}
3334

3435
@Test
@@ -56,18 +57,11 @@ public void givenAList_whenConvertWithGuava_thenReturnMapWithTheSameElements() {
5657
}
5758

5859
@Test
59-
public void givenAList_whenConvertWithApacheCommons1_thenReturnMapWithTheSameElements() {
60+
public void givenAList_whenConvertWithApacheCommons_thenReturnMapWithTheSameElements() {
6061

61-
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons1(list);
62+
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons(list);
6263

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

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-
}
7367
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.convertlisttomap;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.hasItem;
5+
import static org.hamcrest.Matchers.hasSize;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
public class ConvertListWithDiplicatedIdToMapServiceUnitTest {
15+
List<Animal> duplicatedIdList;
16+
17+
private ConvertListToMapService convertListService = new ConvertListToMapService();
18+
19+
@Before
20+
public void init() {
21+
22+
this.duplicatedIdList = new ArrayList<>();
23+
24+
Animal cat = new Animal(1, "Cat");
25+
duplicatedIdList.add(cat);
26+
Animal dog = new Animal(2, "Dog");
27+
duplicatedIdList.add(dog);
28+
Animal pig = new Animal(3, "Pig");
29+
duplicatedIdList.add(pig);
30+
Animal cow = new Animal(4, "Cow");
31+
duplicatedIdList.add(cow);
32+
Animal goat = new Animal(4, "Goat");
33+
duplicatedIdList.add(goat);
34+
35+
}
36+
37+
@Test
38+
public void givenADupIdList_whenConvertBeforeJava8_thenReturnMapWithRewrittenElement() {
39+
40+
Map<Integer, Animal> map = convertListService.convertListBeforeJava8(duplicatedIdList);
41+
42+
assertThat(map.values(), hasSize(4));
43+
assertThat(map.values(), hasItem(duplicatedIdList.get(4)));
44+
}
45+
46+
@Test
47+
public void givenADupIdList_whenConvertWithApacheCommons_thenReturnMapWithRewrittenElement() {
48+
49+
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons(duplicatedIdList);
50+
51+
assertThat(map.values(), hasSize(4));
52+
assertThat(map.values(), hasItem(duplicatedIdList.get(4)));
53+
}
54+
55+
@Test(expected = IllegalStateException.class)
56+
public void givenADupIdList_whenConvertAfterJava8_thenException() {
57+
58+
convertListService.convertListAfterJava8(duplicatedIdList);
59+
}
60+
61+
@Test(expected = IllegalArgumentException.class)
62+
public void givenADupIdList_whenConvertWithGuava_thenException() {
63+
64+
convertListService.convertListWithGuava(duplicatedIdList);
65+
66+
}
67+
68+
}

0 commit comments

Comments
 (0)