1
0
Fork 0
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:
Paul Schaub 2023-06-05 19:44:47 +02:00
parent 41d734f2db
commit 324302c536
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 34 additions and 0 deletions

View file

@ -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) {

View file

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