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
Expand Up @@ -5,14 +5,15 @@
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>();

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

for (Animal animal : list) {
map.put(animal.getId(), animal);
}
Expand All @@ -30,20 +31,9 @@ public Map<Integer, Animal> convertListWithGuava(List<Animal> list) {
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) {
public Map<Integer, Animal> convertListWithApacheCommons(List<Animal> list) {

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void init() {
list.add(cow);
Animal goat = new Animal(5, "Goat");
list.add(goat);

}

@Test
Expand Down Expand Up @@ -56,18 +57,11 @@ public void givenAList_whenConvertWithGuava_thenReturnMapWithTheSameElements() {
}

@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()));
}

@Test
public void givenAList_whenConvertWithApacheCommons2_thenReturnMapWithTheSameElements() {

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

assertThat(map.values(), containsInAnyOrder(list.toArray()));
}
}
Original file line number Diff line number Diff line change
@@ -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);

}

}