Skip to content
Advertisement

Switching Keys in a Map of Maps

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…

JavaScript

Using nested for loops (untested):

JavaScript

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:

JavaScript

Advertisement

Answer

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

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement