Switching Keys in a Map of Maps

Tags: , , ,

How would you use Java 8 streams to swap keys in this map of maps? Or at least clean up this mess a little bit…

    Map<Type1, Map<Type2, String>> to Map<Type2, Map<Type1, String>>

Using nested for loops (untested):

    Map<Type1, Map<Type2, String>> map ...
    Map<Type2, Map<Type1, String>> map2 = new HashMap<>();
        for (Type 1 type1 : map.keySet()) {
            for(Entry<Type2, String> entry : map.get(type1)) {
                if (map2.get(entry.key() == null) {
                    map2.push(entry.key(), new HashMap<Type1, String>();
             map2.get(entry.key()).put(type1, entry.value();

So far I think you would need to flap map into all unique combinations of Type1, Type2, and String and store this set in some sort of intermediate collection.

Definitely wrong:

map.entrySet().stream().flatMap(t -> <Type1, Type2, 
String>).collect(Collectors.toMap(t -> t.Type2, Collectors.toMap(t -> 
t.type1, t->t.String))


Streams aren’t well-suited for this type of problem. Instead, consider using other java 8 additions — Map#forEach and Map#computeIfAbsent:

map.forEach( (t1, e) -> 
    e.forEach( (t2, v) ->
            result.computeIfAbsent(t2, x -> new HashMap<>()).put(t1, v)

Source: stackoverflow