Merge maps by key
Solution 1:
scala.collection.immutable.IntMap
has an intersectionWith
method that does precisely what you want (I believe):
import scala.collection.immutable.IntMap
val a = IntMap(1 -> "one", 2 -> "two", 3 -> "three", 4 -> "four")
val b = IntMap(1 -> "un", 2 -> "deux", 3 -> "trois")
val merged = a.intersectionWith(b, (_, av, bv: String) => Seq(av, bv))
This gives you IntMap(1 -> List(one, un), 2 -> List(two, deux), 3 -> List(three, trois))
. Note that it correctly ignores the key that only occurs in a
.
As a side note: I've often found myself wanting the unionWith
, intersectionWith
, etc. functions from Haskell's Data.Map
in Scala. I don't think there's any principled reason that they should only be available on IntMap
, instead of in the base collection.Map
trait.
Solution 2:
val a = Map(1 -> "one", 2 -> "two", 3 -> "three")
val b = Map(1 -> "un", 2 -> "deux", 3 -> "trois")
val c = a.toList ++ b.toList
val d = c.groupBy(_._1).map{case(k, v) => k -> v.map(_._2).toSeq}
//res0: scala.collection.immutable.Map[Int,Seq[java.lang.String]] =
//Map((2,List(two, deux)), (1,List(one, un), (3,List(three, trois)))
Solution 3:
Scalaz adds a method |+|
for any type A
for which a Semigroup[A]
is available.
If you mapped your Maps so that each value was a single-element sequence, then you could use this quite simply:
scala> a.mapValues(Seq(_)) |+| b.mapValues(Seq(_))
res3: scala.collection.immutable.Map[Int,Seq[java.lang.String]] = Map(1 -> List(one, un), 2 -> List(two, deux), 3 -> List(three, trois))
Solution 4:
Starting Scala 2.13
, you can use groupMap
which (as its name suggests) is an equivalent of a groupBy
followed by map
on values:
// val map1 = Map(1 -> "one", 2 -> "two", 3 -> "three")
// val map2 = Map(1 -> "un", 2 -> "deux", 3 -> "trois")
(map1.toSeq ++ map2).groupMap(_._1)(_._2)
// Map(1 -> List("one", "un"), 2 -> List("two", "deux"), 3 -> List("three", "trois"))
This:
Concatenates the two maps as a sequence of tuples (
List((1, "one"), (2, "two"), (3, "three"))
). For conciseness,map2
is implicitly converted toSeq
to align withmap1.toSeq
's type - but you could choose to make it explicit by usingmap2.toSeq
.group
s elements based on their first tuple part (_._1
) (group part of groupMap)map
s grouped values to their second tuple part (_._2
) (map part of groupMap)