Object 的 .equals 和 .hashCode 将如何适用于我的类?

发布于 2024-10-01 21:57:19 字数 266 浏览 2 评论 0 原文

假设我有自己的类,

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

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

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

Say I have my own class

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

It has some attributes and methods. It DOES NOT implement equals, DOES NOT implement 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雨后彩虹 2024-10-08 21:57:19

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

来自文档:

等于

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

哈希码

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

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).

From the documentation:

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).

hashCode

As far as is reasonably practical, the hashCode method defined by class Object returns 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.)

奈何桥上唱咆哮 2024-10-08 21:57:19

来自 JVM 实现之一中的对象

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

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

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

From Object in one of the JVM implementations:

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.

左耳近心 2024-10-08 21:57:19

Object 中有 equals()hashCode() 的默认实现。如果您不提供自己的实现,那么将使用它们。对于 equals() 来说,这意味着 == 比较:只有当对象完全相同时,它们才相等。对于 hashCode()Javadoc 有很好的解释。

有关详细信息,请参阅《Effective Java》第 3 章 (pdf),项目8.

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 Javadoc has a good explanation.

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

北笙凉宸 2024-10-08 21:57:19

是的,来自 Object 类,因为您的类隐式扩展了 Object。 equals 只是返回 this == objhashCode 实现是本机的。只是猜测 - 它返回指向对象的指针。

Yes, from Object class since your class extends Object implicitly. equals simply returns this == obj. hashCode implementation is native. Just a guess - it returns the pointer to the object.

后eg是否自 2024-10-08 21:57:19

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

由于 HashCodeBuilderApache Commons Lang 的 ">EqualsBuilder。

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.

It is quite easy to provide your own implementation of these methods thanks to HashCodeBuilder and EqualsBuilder from Apache Commons Lang.

北城半夏 2024-10-08 21:57:19

IBM 的 developerworks 说:

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

然而,为了确保特定供应商的 Java 版本的确切实现细节,最好查看源代码(如果可用)

IBM's developerworks says:

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.

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文