Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of duplicate map keys from multiple maps [duplicate]

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?

like image 682
villabilla Avatar asked Sep 13 '25 13:09

villabilla


2 Answers

@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.

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.

like image 31
YCF_L Avatar answered Sep 16 '25 02:09

YCF_L