The scala API let's you append one map to another as follows:
import scala.collection.mutable.{Map => MutableMap}
val m1: MutableMap[Int,String] = MutableMap(1 -> "A", 2 -> "B", 3 -> "C")
val m2: MutableMap[Int,String] = MutableMap(2 -> "X", 3 -> "Y", 4 -> "Z")
m1 ++= m2 // outputs: Map(2 -> X, 4 -> Z, 1 -> A, 3 -> Y)
m1 // outputs: Map(2 -> X, 4 -> Z, 1 -> A, 3 -> Y)
The behaviour is to override the repeated pairs with the pairs coming from the right map.
What is a good way to do it in the opposite way? That is, concatenating the pairs of m1
and m2
in m1
where the pairs of m1
are kept if repeated in m2
.