How can I create a new map from two maps of maps so that the resulting map only includes matches where keys are the same and combines the internal maps.
Iter
I have been playing with something similar to convert a Seq(Map("one" -> 1), Map("two" -> 2))
to Map("one" -> 1, "two" -> 2)
.
I have been looking at previous answers and thought it was too difficult. After playing a bit I found that this solution works and it easy:
val seqOfMaps = Seq(Map("one" -> 1), Map("two" -> 2))
seqOfMaps: Seq[scala.collection.immutable.Map[String,Int]] = List(Map(one -> 1), Map(two -> 2))
val allInOneMap = seqOfMaps.flatten.toMap
allInOneMap: scala.collection.immutable.Map[String,Int] = Map(one -> 1, two -> 2)
The advantage of this approach is that possible empty maps automatically get filtered out.