4

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?

villabilla
  • 55
  • 6
  • you don't need to check if Set contains the key as Set items are unique. Just iterate throw the all keys, add them to the set and use the result. – Sergey Grinev Dec 16 '20 at 07:00

3 Answers3

2

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.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Wow, thank you. Can you explain a little bit how this works, especially the "Collectors.toMap(Function.identity(), x -> false, (a, b) -> true)"? – villabilla Dec 16 '20 at 07:30
  • @villabilla Yes It is a trick to collect as a Map and in the value part you just use a true for duplicate and false for not duplicate – Youcef LAIDANI Dec 16 '20 at 07:48
  • Can you please explain how the following part works (technically) Collectors.toMap(Function.identity(), x -> false, (a, b) -> true) – Panagiotis Bougioukos Dec 16 '20 at 08:34
  • @PanagiotisBougioukos the `x -> false` mean if there are a unique element then return false, the `(a, b) -> true` mean if a and b are equals this mean there are duplicate so return true, for more details [read this](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#toMap-java.util.function.Function-java.util.function.Function-java.util.function.BinaryOperator-java.util.function.Supplier-) – Youcef LAIDANI Dec 16 '20 at 08:50
2
@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.

spongebob
  • 8,370
  • 15
  • 50
  • 83
0

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());
} 
K.Nicholas
  • 10,956
  • 4
  • 46
  • 66