java - 重写 equals 和 hashCode 只是为了调用 super.equals/hashCode 或抛出 AssertionError?

标签 java

何时覆盖 equals 是否有最佳实践?

我应该重写 equals/hashCode 并抛出 AssertionError 吗?只是为了确保没有人使用它? (如《Effective Java》一书中所推荐)

我是否应该重写 equals/hashCode 并仅调用 super.equals/hashCode,因为父类(super class)行为与所需的行为相同? (FindBugs 推荐这个,因为我添加了一个字段)

它们真的是最佳实践吗?

最佳答案

错误

您不应该从 equals 抛出异常或hashCode方法。 equals 和 hashCode 方法随处可见,在这里不加区别地抛出异常可能会在以后对您造成伤害。

断言错误

你永远不应该抛出 AssertionError直接地。推杆assert任何方法中的语句都可以,因为当断言关闭时这些语句将不会运行。

何时覆盖

覆盖并直接传递到 super.equals() 没有什么坏处。如果父类(super class)方法是 Object.equals .

如果您要比较的两个对象属于不同类型,那么您可能会陷入对称性破缺的陷阱,即 x.equals(y)是真的,但是 y.equals(x)是假的。如果 y 是 x 的子类,则可能会发生这种情况,因此 y 的 equals 方法可能会进行稍微不同的比较。您可以使用 if (getClass() != obj.getClass()) return false; 在 equals 方法中检查这一点.

最佳实践

Effective Java这里是一个很棒的资源,特别是关于如何最好地实现 hashCode()方法。请务必阅读并考虑 equals 的契约(Contract)和 hashCode在 Javadoc 中列出,并确保您覆盖其中一个,然后覆盖另一个。 Eclipse 中的“生成 hashCode() 和 equals”函数很好地提供了您在这些方法中所需的内容,因此请检查它为您的类生成的代码。以下内容摘自Javadoc of java.lang.Object .

<强> equals Contract:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

<强> hashCode Contract:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

关于java - 重写 equals 和 hashCode 只是为了调用 super.equals/hashCode 或抛出 AssertionError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3223061/

相关文章:

java - 自动生成代码并出现错误

java - 如何在 Eclipse 中构建 Java Servlet?

java - 是否有支持 NIST 标准的随机生成器的 Java 8 实现?

java - 使用restTemplate的Post方法,错误java.net.UnknownHostException

java - 在另一个 log4j 项目中使用 jar 时,不会创建来自外部 jar 的 log4j 日志文件

java - 在后台接收 GPS 位置更新

java - 循环没有中断 - 使用 GSON 解析 JSON

java - 无需身份验证的 GAE + GWT Servlet

java - ITEXT 7 (Java) - 反向渲染单元格(从右到左)

java - 里氏替换原则 - 我在这里违反了它吗?