Skip to content

Commit 94e0755

Browse files
andrea-ligiospivovarit
authored andcommitted
BAEL-1698 (eugenp#4134)
* Also needed * BAEL-1698
1 parent 40d6af7 commit 94e0755

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.baeldung.java.map;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import org.apache.commons.collections4.MultiMap;
12+
import org.apache.commons.collections4.MultiMapUtils;
13+
import org.apache.commons.collections4.MultiValuedMap;
14+
import org.apache.commons.collections4.map.MultiValueMap;
15+
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
16+
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
17+
import org.junit.Test;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import com.google.common.collect.ArrayListMultimap;
22+
import com.google.common.collect.LinkedHashMultimap;
23+
import com.google.common.collect.Multimap;
24+
import com.google.common.collect.TreeMultimap;
25+
26+
public class MapMultipleValuesTest {
27+
private static final Logger LOG = LoggerFactory.getLogger(MapMultipleValuesTest.class);
28+
29+
@Test
30+
public void givenHashMap_whenPuttingTwice_thenReturningFirstValue() {
31+
Map<String, String> map = new HashMap<>();
32+
assertThat(map.put("key1", "value1")).isEqualTo(null);
33+
assertThat(map.put("key1", "value2")).isEqualTo("value1");
34+
assertThat(map.get("key1")).isEqualTo("value2");
35+
}
36+
37+
@Test
38+
public void givenCollectionAsValue_whenPuttingTwice_thenReturningCollection() {
39+
Map<String, List<String>> map = new HashMap<>();
40+
List<String> list = new ArrayList<>();
41+
map.put("key1", list);
42+
map.get("key1").add("value1");
43+
map.get("key1").add("value2");
44+
assertThat(map.get("key1").get(0)).isEqualTo("value1");
45+
assertThat(map.get("key1").get(1)).isEqualTo("value2");
46+
}
47+
48+
@Test
49+
public void givenMultiValueMap_whenPuttingTwice_thenReturningValues() {
50+
MultiMap<String, String> map = new MultiValueMap<>();
51+
map.put("key1", "value1");
52+
map.put("key1", "value2");
53+
assertThat((Collection<String>) map.get("key1"))
54+
.contains("value1", "value2");
55+
}
56+
57+
@Test
58+
public void givenArrayListValuedHashMap_whenPuttingDoubleValues_thenReturningAllValues() {
59+
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
60+
map.put("key1", "value1");
61+
map.put("key1", "value2");
62+
map.put("key1", "value2");
63+
assertThat((Collection<String>) map.get("key1"))
64+
.containsExactly("value1", "value2", "value2");
65+
}
66+
67+
@Test
68+
public void givenHashSetValuedHashMap_whenPuttingTwiceTheSame_thenReturningOneValue() {
69+
MultiValuedMap<String, String> map = new HashSetValuedHashMap<>();
70+
map.put("key1", "value1");
71+
map.put("key1", "value1");
72+
assertThat((Collection<String>) map.get("key1"))
73+
.containsExactly("value1");
74+
}
75+
76+
@Test(expected = UnsupportedOperationException.class)
77+
public void givenUnmodifiableMultiValuedMap_whenInserting_thenThrowingException() {
78+
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
79+
map.put("key1", "value1");
80+
map.put("key1", "value2");
81+
MultiValuedMap<String, String> immutableMap =
82+
MultiMapUtils.unmodifiableMultiValuedMap(map);
83+
immutableMap.put("key1", "value3");
84+
}
85+
86+
@Test
87+
public void givenArrayListMultiMap_whenInserting_thenCorrectOutput() {
88+
Multimap<String, String> map = ArrayListMultimap.create();
89+
map.put("key1", "value2");
90+
map.put("key1", "value1");
91+
assertThat((Collection<String>) map.get("key1"))
92+
.containsExactly("value2", "value1");
93+
}
94+
95+
@Test
96+
public void givenLinkedHashMultiMap_whenInserting_thenReturningValuesInInsertionOrder() {
97+
Multimap<String, String> map = LinkedHashMultimap.create();
98+
map.put("key1", "value3");
99+
map.put("key1", "value1");
100+
map.put("key1", "value2");
101+
assertThat((Collection<String>) map.get("key1"))
102+
.containsExactly("value3", "value1", "value2");
103+
}
104+
105+
@Test
106+
public void givenTreeMultimap_whenInserting_thenReturningValuesInNaturalOrder() {
107+
Multimap<String, String> map = TreeMultimap.create();
108+
map.put("key1", "value3");
109+
map.put("key1", "value1");
110+
map.put("key1", "value2");
111+
assertThat((Collection<String>) map.get("key1"))
112+
.containsExactly("value1", "value2", "value3");
113+
}
114+
115+
}

0 commit comments

Comments
 (0)