I have an Array of Map of Maps and would like to merge the like keys and combine their values into a list. Is there an elegant way to do this, preferably without introducing any new libraries or updating scala? Scala version 2.11.12
(Map("a" -> Map(1 -> 1.1)),
Map("a" -> Map(2 -> 2.1)),
Map("b" -> Map(1 -> 1.1)),
Map("c" -> Map(1 -> 1.1)),
Map("c" -> Map(2 -> 2.2)))
Output:
Map(
"a" -> List(Map(1 -> 1.1), Map(2 -> 2.1)),
"b" -> List(Map(1 -> 1.1)),
"c" -> List(Map(1 -> 1.1), Map(2 -> 2.2)))
Edit to explain why it's not a duplicate of: Scala: Merge map That answer does not dynamically go through a whole list and merge identical keys, it only merges 2 specific maps given. As exemplified by the answer, the real solution to this has to do with an elegant way of iterating through the list and not only merging lists, but first loading the maps into a list and then merging them.