scala seq java_Scala中的两个Seq比较

如果我理解你的话,你已经通过覆盖 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值