Bael 2018 (#4774)
* BAEL-2018 * Update Animal.java * rename * tests added * generic type shorten
This commit is contained in:
parent
b501487e0f
commit
8f52c3f6d3
@ -5,14 +5,15 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.commons.collections4.IterableUtils;
|
|
||||||
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.collections4.MapUtils;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
public class ConvertListToMapService {
|
public class ConvertListToMapService {
|
||||||
|
|
||||||
public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
|
public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
|
||||||
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
|
|
||||||
|
Map<Integer, Animal> map = new HashMap<>();
|
||||||
|
|
||||||
for (Animal animal : list) {
|
for (Animal animal : list) {
|
||||||
map.put(animal.getId(), animal);
|
map.put(animal.getId(), animal);
|
||||||
}
|
}
|
||||||
@ -30,20 +31,9 @@ public class ConvertListToMapService {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<Integer, Animal> convertListWithApacheCommons1(List<Animal> list) {
|
public Map<Integer, Animal> convertListWithApacheCommons(List<Animal> list) {
|
||||||
|
|
||||||
Map<Integer, Animal> map = new HashMap<Integer, Animal>();
|
Map<Integer, Animal> map = new HashMap<>();
|
||||||
|
|
||||||
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);
|
MapUtils.populateMap(map, list, Animal::getId);
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ public class ConvertListToMapServiceUnitTest {
|
|||||||
list.add(cow);
|
list.add(cow);
|
||||||
Animal goat = new Animal(5, "Goat");
|
Animal goat = new Animal(5, "Goat");
|
||||||
list.add(goat);
|
list.add(goat);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -56,18 +57,11 @@ public class ConvertListToMapServiceUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAList_whenConvertWithApacheCommons1_thenReturnMapWithTheSameElements() {
|
public void givenAList_whenConvertWithApacheCommons_thenReturnMapWithTheSameElements() {
|
||||||
|
|
||||||
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons1(list);
|
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons(list);
|
||||||
|
|
||||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
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()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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<Animal> 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<Integer, Animal> map = convertListService.convertListBeforeJava8(duplicatedIdList);
|
||||||
|
|
||||||
|
assertThat(map.values(), hasSize(4));
|
||||||
|
assertThat(map.values(), hasItem(duplicatedIdList.get(4)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenADupIdList_whenConvertWithApacheCommons_thenReturnMapWithRewrittenElement() {
|
||||||
|
|
||||||
|
Map<Integer, Animal> 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user