scala.collection.immutable.IntMap http://www.scala-lang.org/api/current/scala/collection/immutable/IntMap.html has an intersectionWith
完全符合你想要的方法(我相信):
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))
这给你IntMap(1 -> List(one, un), 2 -> List(two, deux), 3 -> List(three, trois))
。请注意,它正确地忽略了仅出现在a
.
附带说明:我经常发现自己想要unionWith
, intersectionWith
等函数来自哈斯克尔的Data.Map http://hackage.haskell.org/packages/archive/containers/0.4.2.0/doc/html/Data-Map.html在斯卡拉。我不认为有任何原则性的理由表明它们应该只在IntMap
,而不是在基地collection.Map
trait.