mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-26 12:58:02 +01:00
Add MultiMap.flatten()
This commit is contained in:
parent
41d734f2db
commit
324302c536
2 changed files with 34 additions and 0 deletions
|
@ -100,6 +100,19 @@ public class MultiMap<K, V> {
|
|||
return map.entrySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all values of the {@link MultiMap} in a single {@link LinkedHashSet}.
|
||||
*
|
||||
* @return set of all values
|
||||
*/
|
||||
public Set<V> flatten() {
|
||||
LinkedHashSet<V> flattened = new LinkedHashSet<>();
|
||||
for (Set<V> items : map.values()) {
|
||||
flattened.addAll(items);
|
||||
}
|
||||
return flattened;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null) {
|
||||
|
|
|
@ -14,6 +14,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -156,4 +158,23 @@ public class MultiMapTest {
|
|||
assertFalse(map.containsKey("foo"));
|
||||
assertTrue(map.containsKey("bingo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void flattenEmptyMap() {
|
||||
MultiMap<String, String> empty = new MultiMap<>();
|
||||
assertEquals(Collections.emptySet(), empty.flatten());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void flattenMap() {
|
||||
MultiMap<String, String> map = new MultiMap<>();
|
||||
map.put("A", "1");
|
||||
map.put("A", "2");
|
||||
map.put("B", "1");
|
||||
|
||||
Set<String> expected = new LinkedHashSet<>();
|
||||
expected.add("1");
|
||||
expected.add("2");
|
||||
assertEquals(expected, map.flatten());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue