合并scala映射并根据条件更新公共密钥

合并scala映射并根据条件更新公共密钥,scala,Scala,我编写了以下代码来合并映射和更新公共密钥。有没有更好的方法写这个 case class Test(index: Int, min: Int, max: Int, aggMin: Int, aggMax: Int) def mergeMaps(oldMap: Map[Int, Test], newMap: Map[Int, Test]): Map[Int, Test] = { val intersect: Map[Int, Test] = oldMap.keySet.interse

我编写了以下代码来合并映射和更新公共密钥。有没有更好的方法写这个

  case class Test(index: Int, min: Int, max: Int, aggMin: Int, aggMax: Int)
  def mergeMaps(oldMap: Map[Int, Test], newMap: Map[Int, Test]): Map[Int, Test] = {
    val intersect: Map[Int, Test] = oldMap.keySet.intersect(newMap.keySet)
      .map(indexKey => indexKey -> (Test(newMap(indexKey).index, newMap(indexKey).min, newMap(indexKey).max,
        oldMap(indexKey).aggMin.min(newMap(indexKey).aggMin), oldMap(indexKey).aggMax.max(newMap(indexKey).aggMax)))).toMap
    val merge = (oldMap ++ newMap ++ intersect)
    merge
  }
这是我的测试用例

it("test my case"){
  val oldMap = Map(10 -> Test(10, 1, 2, 1, 2), 25 -> Test(25, 3, 4, 3, 4), 46 -> Test(46, 3, 4, 3, 4), 26 -> Test(26, 1, 2, 1, 2))
  val newMap = Map(32 -> Test(32, 5, 6, 5, 6), 26 -> Test(26, 5, 6, 5, 6))
  val result = mergeMaps(oldMap, newMap)
  //Total elements count should be map 1 elements + map 2 elements
  assert(result.size == 5)
  //Common key element aggMin and aggMax should be updated, keep min aggMin and max aggMax from 2 common key elements and keep min and max of second map key
  assert(result.get(26).get.aggMin == 1)//min aggMin -> min(1,5)
  assert(result.get(26).get.aggMax == 6)//max aggMax -> max(2,6)
  assert(result.get(26).get.min == 5)// 5 from second map
  assert(result.get(26).get.max == 6)//6 from second map
}

这里有一个稍微不同的解决方案

def mergeMaps(oldMap :Map[Int,Test], newMap :Map[Int,Test]) :Map[Int,Test] =
  (oldMap.values ++ newMap.values)
    .groupBy(_.index)
    .map{ case (k,v) =>
      k -> v.reduceLeft((a,b) => 
          Test(k, b.min, b.max, a.aggMin min b.aggMin, a.aggMax max b.aggMax))
    }

我本可以用mapValues而不是map跟随groupBy,但那是另一个版本来完成同样的任务

    def mergeMaps(oldMap: Map[Int, Test], newMap: Map[Int, Test]): Map[Int, Test] = {

          (newMap ++ oldMap).map(key => {
            val _newMapData = newMap.get(key._1)
            if (_newMapData.isDefined) {
              val _newMapDataValue = _newMapData.get
              val oldMapValue = key._2
              val result = Test(_newMapDataValue.index, _newMapDataValue.min, _newMapDataValue.max,
                oldMapValue.aggMin.min(_newMapDataValue.aggMin), oldMapValue.aggMax.max(_newMapDataValue.aggMax))
              (key._1 -> result)
            } else (key._1 -> key._2)
          })

    }

我建议您使用scalaZ库并使用|+|组合方法,这样会更干净。@RamanMishra.我没有scalaZ选项谁否决了它?问题是什么?