BAEL-586 guava multimap examples

This commit is contained in:
Tomasz Lelek 2017-01-12 11:16:34 +01:00
parent a5978cf259
commit 7df755545a
1 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,62 @@
package com.baeldung.guava;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class MultimapTest {
@Test
public void givenMap_whenAddTwoValuesFroSameKey_shouldOverridePreviousKey() {
//given
String key = "a-key";
Map<String, String> map = new LinkedHashMap<>();
//when
map.put(key, "firstValue");
map.put(key, "secondValue");
//then
assertEquals(1, map.size());
}
@Test
public void givenMultiMap_whenAddTwoValuesFroSameKey_shouldHaveTwoEntriesInMap() {
//given
String key = "a-key";
Multimap<String, String> map = ArrayListMultimap.create();
//when
map.put(key, "firstValue");
map.put(key, "secondValue");
//then
assertEquals(2, map.size());
}
@Test
public void givenMapOfListValues_whenAddTwoValuesFroSameKey_shouldHaveTwoElementInList() {
//given
String key = "a-key";
Map<String, List<String>> map = new LinkedHashMap<>();
//when
List<String> values = map.get(key);
if(values == null){
values = new LinkedList<>();
values.add("firstValue");
values.add("secondValue");
}
map.put(key, values);
//then
assertEquals(1, map.size());
}
}