Java 默认的 .equals 和 .hashCode 如何为我的类工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4178997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:23:21  来源:igfitidea点击:

How default .equals and .hashCode will work for my classes?

javaobjectequalshashcodeequality

提问by alexeypro

Say I have my own class

说我有自己的班级

public class MyObj { /* ... */ }

It has some attributes and methods. It DOES NOT implement equals, DOES NOT implement hashCode.

它有一些属性和方法。它不实现equals,不实现hashCode。

Once we call equals and hashCode, what are the default implementations? From Object class? And what are they? How the default equals will work? How the default hashCode will work and what will return? == will just check if they reference to the same object, so it's easy, but what about equals() and hashCode() methods?

一旦我们调用了 equals 和 hashCode,默认的实现是什么?从对象类?他们是什么?默认的 equals 将如何工作?默认的 hashCode 将如何工作以及将返回什么?== 只会检查它们是否引用了同一个对象,所以这很容易,但是 equals() 和 hashCode() 方法呢?

采纳答案by Etienne de Martel

Yes, the default implementation is Object's (generally speaking; if you inherit from a class that redefined equals and/or hashCode, then you'll use that implementation instead).

是的,默认实现是 Object 的(一般来说;如果您从重新定义 equals 和/或 hashCode 的类继承,那么您将改用该实现)。

From the documentation:

从文档:

equals

equals

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Object 类的 equals 方法实现了对象上最有区别的可能等价关系;也就是说,对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象(x == y 的值为 true)时,此方法才返回 true。

hashCode

hashCode

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

尽可能实用,类 Object 定义的 hashCode 方法确实为不同的对象返回不同的整数。(这通常通过将对象的内部地址转换为整数来实现,但 JavaTM 编程语言不需要这种实现技术。)

回答by brabster

IBM's developerworkssays:

IBM 的developerworks说:

Under this default implementation, two references are equal only if they refer to the exact same object. Similarly, the default implementation of hashCode() provided by Object is derived by mapping the memory address of the object to an integer value.

在此默认实现下,两个引用仅在引用完全相同的对象时才相等。同样,Object 提供的 hashCode() 的默认实现是通过将对象的内存地址映射到整数值来导出的。

However, to be sure of the exact implementation details for a particular vendor's Java version it's probably best to look as the source (if it's available)

但是,要确定特定供应商的 Java 版本的确切实现细节,最好将其视为源代码(如果可用)

回答by khachik

Yes, from Objectclass since your class extends Object implicitly. equalssimply returns this == obj. hashCodeimplementation is native. Just a guess - it returns the pointer to the object.

是的,从Object类开始,因为您的类隐式扩展了 Object。equals简单地返回this == objhashCode实现是原生的。只是一个猜测 - 它返回指向对象的指针。

回答by Brad Mace

From Objectin one of the JVM implementations:

来自ObjectJVM 实现之一:

public boolean equals(Object object) {
    return this == object;
}

public int hashCode() {
    return VMMemoryManager.getIdentityHashCode(this);
}

In both cases it's just comparing the memory addresses of the objects in question.

在这两种情况下,它只是比较相关对象的内存地址。

回答by Jorn

There are default implementations of equals()and hashCode()in Object. If you don't provide your own implementation, those will be used. For equals(), this means an ==comparison: the objects will only be equal if they are exactly the same object. For hashCode(), the Javadochas a good explanation.

还有的默认实现equals(),并hashCode()在对象。如果您不提供自己的实现,则将使用这些实现。对于equals(),这意味着==比较:只有当它们是完全相同的对象时,对象才会相等。对于hashCode()Javadoc有很好的解释。

For more information, see Effective Java, Chapter 3(pdf), item 8.

有关更多信息,请参阅 Effective Java,第 3 章(pdf),第 8 项。

回答by Pawe? Dyda

If you do not provide your own implementation, one derived from Object would be used. It is OK, unless you plan to put your class instances into i.e. HashSet (any collection that actually use hashCode() ), or something that need to check object's equality (i.e. HashSet's contains() method). Otherwise it will work incorrectly, if that's what you are asking for.

如果您不提供自己的实现,则将使用从 Object 派生的实现。没关系,除非您打算将您的类实例放入 ie HashSet(任何实际使用 hashCode() 的集合),或者需要检查对象相等性的东西(即 HashSet 的 contains() 方法)。否则它将无法正常工作,如果这就是您所要求的。

It is quite easy to provide your own implementation of these methods thanks to HashCodeBuilderand EqualsBuilderfrom Apache Commons Lang.

这是很容易给自己的机制,这些得益于方法的HashCodeBuilderEqualsBuilder阿帕奇共享郎