首页 文章

Scala中的两个Seq比较

提问于
浏览
2

以下是确定数据更新中的更新数据的方案 . 一个案例类对象的Seq有它们的ID,case类有一个override equals方法,它对除了ID字段之外的属性进行数据比较 . 要找出任何更新的数据条目,我需要从DB检索数据 . 并且需要比较两个序列 . 什么是Scala方法来找出任何更新的对象?

只是想一想:创建一个以对象ID为键,对象为其值的 Map . 那可能有用 .

(更新)这是我出来的解决方案

val existingDataList: Foo = ...
val existingDataMap: Map[Long, Foo] = existingDataList.map(d => d.id -> d)(collection.breakOut)

// To find out updated data    
val updatedData = inputData.filter(d => existingDataMap.get(d.id) != d)

2 回答

  • 3

    所以,你有两个集合,你想在其中找到对象,具有相同的ID,但数据不同,对吧? diff 在这里真的不是你想要的 .

    像这样的东西会这样做:

    (first ++ second)
      .groupBy (_.id)
      .mapValues (_.toSet)
      .filterNot { case (_, v) => v.size != 2 }
      .values
      .map { v => v.head -> v.last }
    

    它将为您提供一个元组列表,如(第一个,第二个),其中两个元素具有相同的ID,但数据不同 .

    这假设您的ID在每个集合中都是唯一的,并且每个ID都出现在两个集合中 .

    或者,如果您可以保证集合具有相同的大小,并且包含完全相同的ID集合,那么您可以执行类似这样的操作,这样效率较低,但更简单一点:

    first.sortBy(_.id)
           .zip(second.sortBy(_.id))
           .filterNot { case (a, b) => a == b }
    
  • 4

    如果我理解你的话,你已经通过覆盖 equals 完成了大部分的努力 - 当然你 MUST 也相应地覆盖 hashCode 例如:

    case class Thing(id:Long, foo:String, bar:Int) {
      override def equals(that:Any):Boolean = {
        that match {
          case Thing(_, f, b) => (f == this.foo) && (b == this.bar)
          case _ => false
        }
      }
    
      override def hashCode:Int = {
        // A quick hack. You probably shouldn't do this for real; 
        // set id to 0 and use the default hashing algorithm:
        ScalaRunTime._hashCode(this.copy(id = 0))
      }
    }
    

    现在我们定义一些 Thing 实例:

    val t1 = Thing(111, "t1", 1)
    val t1c = Thing(112, "t1", 1) // Same as t1 but with a new ID
    val t2 = Thing(222, "t2", 2)
    val t3 = Thing(333, "t3", 3)
    val t4 = Thing(444, "t4", 4)
    val t4m = Thing(444, "t4m", 4)  // Same as t4 but with a modified "foo"
    

    让我们做几个序列:

    val orig = Seq(t1, t2, t3, t4)
    val mod = Seq(t1c, t2, t3, t4m)
    

    现在diff告诉我们我们需要知道的一切:

    mod.diff(orig)
    // => returns Seq(t4m) - just what we wanted
    

相关问题