压缩两个HashMaps(或词典)

时间:2013-04-28 00:13:21

标签: scala map functional-programming

在Scala中压缩两个词典的功能方法是什么?

map1 = new HashMap("A"->1,"B"->2)
map2 = new HashMap("B"->22,"D"->4) // B is the only common key

zipper(map1,map2)应该提供与

类似的内容
 Seq( ("A",1,0), // no A in second map, so third value is zero
      ("B",2,22),
      ("D",0,4)) // no D in first map, so second value is zero 

如果不起作用,也赞赏任何其他风格

2 个答案:

答案 0 :(得分:18)

def zipper(map1: Map[String, Int], map2: Map[String, Int]) = {
  for(key <- map1.keys ++ map2.keys)
    yield (key, map1.getOrElse(key, 0), map2.getOrElse(key, 0))
}


scala> val map1 = scala.collection.immutable.HashMap("A" -> 1, "B" -> 2)
map1: scala.collection.immutable.HashMap[String,Int] = Map(A -> 1, B -> 2)

scala> val map2 = scala.collection.immutable.HashMap("B" -> 22, "D" -> 4)
map2: scala.collection.immutable.HashMap[String,Int] = Map(B -> 22, D -> 4)

scala> :load Zipper.scala
Loading Zipper.scala...
zipper: (map1: Map[String,Int], map2: Map[String,Int])Iterable[(String, Int, Int)]

scala> zipper(map1, map2)
res1: Iterable[(String, Int, Int)] = Set((A,1,0), (B,2,22), (D,0,4))

在这种情况下,使用get注意可能优于getOrElseNone用于指定值不存在而不是使用0

答案 1 :(得分:0)

作为布赖恩答案的替代,这可以用于通过隐式方法增强地图类:

implicit class MapUtils[K, +V](map: collection.Map[K, V]) {
  def zipAllByKey[B >: V, C >: V](that: collection.Map[K, C], thisElem: B, thatElem: C): Iterable[(K, B, C)] =
  for (key <- map.keys ++ that.keys)
    yield (key, map.getOrElse(key, thisElem), that.getOrElse(key, thatElem))
}

命名和 API 类似于序列 zipAll