数组的Case类相等 [英] Case Class equality for Arrays
问题描述
我有以下案例类
case class Something(val input : Array[Int], val output : Array[Int] = null, val remainder : Array[Int] = null, val next : Something = null) {
override def equals(thatGeneric: scala.Any): Boolean = {
if(!thatGeneric.isInstanceOf[Something])
return false
val that = thatGeneric.asInstanceOf[Something]
val thisInput = if(this.input == null) null else this.input.deep
val thatInput = if(that.input == null) null else that.input.deep
val thisOutput = if(this.output == null) null else this.output.deep
val thatOutput = if(that.output == null) null else that.output.deep
val thisRemainder = if(this.remainder == null) null else this.remainder.deep
val thatRemainder = if(that.remainder == null) null else that.remainder.deep
return (thisInput,thisOutput,thisRemainder,this.next) == (thatInput,thatOutput,thatRemainder,that.next)
}
/**
* TODO fix hashcode in case I want to use this in collection
* @return
*/
override def hashCode(): Int = super.hashCode()
}
我知道案例类应该为您创建equals和hashCode方法,但为数组创建数组,因为 ==
不起作用,那么我猜这就是为什么
I know that case classes are supposed to have your equals and hashCode methods created for you but for Arrays since ==
doesn't work then I guess thats why it doesn't work for my case class.
是否有更好的方法不必为案例类手写我的equals和hashCode方法?
Is there a better way to not have to hand-write my equals and hashCode methods for my case class?
注意:我也欢迎有关为什么/如何改善当前的equals方法的建议
Note: I also welcome advice on why/how to improve my current equals method
编辑:请多谢我的使用注意事项一般情况下是case类,但我的问题基本上是:假设您的case类中需要一个Array,如何避免滚动自己的equals和hashCode。 (如果答案是不可能的,而我需要使用列表,那就是答案,只是不确定是这种情况。)
Do appreciate the notes about my use of case class in general, but my question was basically: Assume you have the need of an Array in your case class, how do you avoid rolling your own equals and hashCode. (If the answer is that its impossible and I need to use a list then that is the answer, just not sure that is the case.)
推荐答案
数组确实更轻巧,但是不幸的是,数组并没有那么多(scala并未为数组重新实现大多数方法,因此它们最终共享了通用的(糟糕的)scala集合性能属性。
Array is indeed lighter, but, unfortunately, not by much (scala doesn't reimplement most of the methods for the array, so they end up sharing the generic (sucky) scala collection performance properties.
只要您设置使用数组,就必须实现自己的 equals
,您所能做的不多关于这一点,但您可以使其看起来更漂亮,例如:
As long as you are set on using arrays, you'll have to implement your own equals
, not much that you can do about that, but you can make it look prettier, like this:
def eq[T](a: Array[T], b: Array[T]) = Option(a.toSeq) == Option(b.toSeq)
def equals(that: Something) =
eq(input, that.input) &&
eq(output, that.output) &&
eq(remainder, that.remainder) &&
next == that.next
您应该考虑将变量设为可选,就像所有人所说的那样, null
不是不去g为您在Scala中得分。
You should consider making your variable optional though. Like everybody says, null
isn't going to score you any points in scala.
同样,就像其他人所说的一样,重新考虑将您的案例类作为一个类:使用案例类的重点是获得 equals
和 hashCode
(以及其他一些东西)是免费的。如果您仍然要重新实现所有这些功能,请将其声明为case类,如果只是造成混淆,并且没有任何好处。
Also, again, like everybody else said, reconsider having your case class as a class: the whole point of using case classes is getting equals
and hashCode
(and some other stuff) for free. If you are going to reimplement all of that anyway, declaring it as a case class if just confusing, and has no benefit.
这篇关于数组的Case类相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!