|
6 | 6 | import static org.hamcrest.CoreMatchers.is;
|
7 | 7 | import static org.hamcrest.CoreMatchers.nullValue;
|
8 | 8 | import static org.hamcrest.MatcherAssert.assertThat;
|
9 |
| -import static org.junit.Assert.assertNull; |
10 |
| -import static org.junit.Assert.assertTrue; |
| 9 | +import static org.junit.Assert.*; |
11 | 10 |
|
12 | 11 | /**
|
13 | 12 | * Created by mtumilowicz on 2018-11-09.
|
@@ -215,6 +214,31 @@ public void computeIfPresent_absent() {
|
215 | 214 |
|
216 | 215 | assertTrue(map.isEmpty());
|
217 | 216 | }
|
| 217 | + |
| 218 | + @Test |
| 219 | + public void computeIfPresent_multimap() { |
| 220 | + Map<Integer, Set<Integer>> map = new HashMap<>(); |
| 221 | + |
| 222 | + map.computeIfAbsent(1, x -> new HashSet<>()).add(1); |
| 223 | + map.get(1).add(2); |
| 224 | + map.get(1).add(3); |
| 225 | + map.get(1).add(4); |
| 226 | + |
| 227 | + map.computeIfPresent(1 , (key, set) -> set.remove(4) && set.isEmpty() ? null : set); |
| 228 | + |
| 229 | + assertThat(map.get(1), is(new HashSet<>(Arrays.asList(1, 2, 3)))); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + public void computeIfPresent_multimap_removeEntry() { |
| 234 | + Map<Integer, Set<Integer>> map = new HashMap<>(); |
| 235 | + |
| 236 | + map.computeIfAbsent(1, x -> new HashSet<>()).add(1); |
| 237 | + |
| 238 | + map.computeIfPresent(1 , (key, set) -> set.remove(1) && set.isEmpty() ? null : set); |
| 239 | + |
| 240 | + assertFalse(map.containsKey(1)); |
| 241 | + } |
218 | 242 |
|
219 | 243 | @Test
|
220 | 244 | public void compute_present_nonNullValue() {
|
|
0 commit comments