[英]Overriding the Equals in Scala
I need to have a custom equal method for a few of case classes. 我需要为一些案例类提供一个自定义的equal方法。 After some online searching, I haven't see any definite solution.
经过一些在线搜索,我还没有确定的解决方案。 Shall I write my own method of equality check?
我应该写自己的相等性检查方法吗?
Yes. 是。 You will need to define your own
equals
method: 您将需要定义自己的
equals
方法:
case class Person(...) {
override def equals(other: Any): Boolean = {
...
}
override def hashCode: Int = {
...
}
}
The equals
method looks easy, but it could also be trick. equals
方法看起来很简单,但也可能是技巧。 I strongly advise you to read the following chapter of Scala Cookbook about how to Define an equals Method (Object Equality) and this essay at Artima: How to Write an Equality Method in Java . 我强烈建议您阅读Scala Cookbook的下一章,该章关于如何定义一个equals方法(对象相等),以及Artima: 如何用Java编写相等方法的这篇文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.