Get list of duplicate map keys from multiple maps

Viewed 401

I have multiple maps of arrays.

Map<String, List<String>> map1 = new HashMap<>();
Map<String, List<String>> map2 = new HashMap<>();
Map<String, List<String>> map3 = new HashMap<>();
Map<String, List<String>> map4 = new HashMap<>();

I want to get the list of duplicate map keys from the multiple maps.

For example

map1.put("k0", Arrays.asList("a0", "a1"));
map1.put("k1", Arrays.asList("b0", "b1"));

map2.put("k1", Arrays.asList("z1", "z2"));
map2.put("k2", Arrays.asList("z1", "z2"));

map3.put("k1", Arrays.asList("z1", "z2"));
map3.put("k3", Arrays.asList("z1", "z2"));

map4.put("k3", Arrays.asList("z1", "z2"));
map4.put("k4", Arrays.asList("z1", "z2"));

map5.put("k0", Arrays.asList("z1", "z2"));
map5.put("k5", Arrays.asList("z1", "z2"));

// Expected output is 
List: [k0, k1, k3]

Aside from iterating through all of the map keys, checking if a set contains the key, and if not adding the key to the set, I can't think of any cleaner ways of doing this. Is there a way to accomplish this with streams?

3 Answers

Because you are using java-stream tag, you can solve your problem like so:

Set<String> duplicateKeys = Stream.of(map1.keySet(), map2.keySet(), map3.keySet(), map4.keySet(), map5.keySet())
        .flatMap(Set::stream)
        .collect(Collectors.toMap(Function.identity(), x -> false, (a, b) -> true))
        .entrySet().stream()
        .filter(Map.Entry::getValue)
        .map(Map.Entry::getKey)
        .collect(Collectors.toSet());

Output

[k0, k1, k3]

.collect(Collectors.toMap(Function.identity(), x -> false, (a, b) -> true)) this return a Map<String, Boolean> it return the key and true if duplicate and false if not, then you just filter to get only the entries where the value is true .filter(Map.Entry::getValue) which represent the duplicate ones.

@SafeVarargs
private static Set<String> findDuplicateKeys(Map<String, ?>... maps) {
    Set<String> keys = new HashSet<>();
    return Arrays.stream(maps)
            .flatMap(map -> map.keySet().stream())
            .filter(key -> !keys.add(key))
            .collect(Collectors.toSet());
}

I would model the result as a set rather than list, since no duplicate elements are possible.

Or, you can try this. Create the stream of maps, then flatMap it to a stream of keys, then collect the keys into a Map counting how many of each key there is, then stream that EntrySet, Set<Map.Entry>, filter by the count of the occurrence of the keys, map that to the key values and collect to a list. This should be able to run with .parallel().

 private List<String> findDups(Map<String, ?>... maps) {
 return Arrays.asList(maps).stream()
  .flatMap(m->m.keySet().stream())
  .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
  .entrySet().stream()
  .filter(es->es.getValue() > 1)
  .map(es->es.getKey())
  .collect(Collectors.toList());
} 
Related