简体   繁体   English

对象等于方法和哈希码方法

[英]Object equals method and hashcode Method

I'm revising for my upcoming exam and I've came across a question from a past paper that I'm unsure on.我正在为即将到来的考试复习,我在过去的一篇论文中遇到了一个我不确定的问题。

The question is the following : Describe the equivalence relation that the Object equals method implements.问题如下: 描述 Object equals 方法实现的等价关系。 What relationship must hold between the Object equals method and hasCode methods? Object equals 方法和 hasCode 方法之间必须保持什么关系?

If this came up in the exam then I wouldn't be too sure how about answering this.如果这在考试中出现,那么我不太确定如何回答这个问题。 I'll try and give it my best shot.我会尽力而为。 Since the Object equals method is checking whether two objects are equal, the hashcode gives objects ascii values.由于 Object equals 方法正在检查两个对象是否相等,因此哈希码给出对象 ascii 值。 If you have two objects that are the same it is possible that they can be given the same hascode since they're equal.如果您有两个相同的对象,则它们可能会被赋予相同的 hascode,因为它们是相等的。 The object equals method you are told whether they're equal whereas the hashcode method gives you a value which you can then use to compare with other hashcodes and find out whether they're the same or not.对象 equals 方法告诉您它们是否相等,而 hashcode 方法为您提供一个值,然后您可以使用该值与其他哈希码进行比较并找出它们是否相同。 Also could have I of mentioned something about overriding methods ?我也可以提到一些关于覆盖方法的内容吗? I'm not if that could come into play in the answer.我不知道这是否可以在答案中发挥作用。

My answer is probably completely wrong but the best I could think off :P我的答案可能完全错误,但我能想到的最好的:P

To the Java API Documentation!到 Java API 文档! especially Object.hashCode特别是Object.hashCode

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.如果根据 equals(Object) 方法两个对象相等,则对两个对象中的每一个调用 hashCode 方法必须产生相同的整数结果。

This means that if the equal method returns true, hashCode must return the same value for both objects.这意味着如果 equal 方法返回 true,hashCode 必须为两个对象返回相同的值。

x.equals(y) == true    =>   x.hashCode() == y.hashCode()

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.如果根据 equals(java.lang.Object) 方法两个对象不相等,则不需要对两个对象中的每一个调用 hashCode 方法必须产生不同的整数结果。 However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.但是,程序员应该意识到为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

But!但! If the objects are not the same they may have the same hashCode()!如果对象不相同,它们可能具有相同的 hashCode()! (But it is not advised) (但不建议)

The requirements for the equals() method are well described, so i won't repeat them here. equals()方法的要求已经很好地描述了,所以我不会在这里重复它们。

The question is clearly answered in the descriptions of both the equals() method and the hashCode() method.这个问题在equals()方法和hashCode()方法的描述中得到了明确的回答。 Have a look at equals method description and hashCode method description .查看equals 方法描述hashCode 方法描述

Here is the relevant answer you might be looking for:以下是您可能正在寻找的相关答案:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; Object 类的 equals 方法实现了对象上最有区别的可能等价关系; 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).也就是说,对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象(x == y 的值为 true)时,此方法才返回 true。

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.请注意,每当重写此方法时,通常都需要重写 hashCode 方法,以维护 hashCode 方法的一般约定,即相等的对象必须具有相等的哈希码。

This the hashCode method description:这个 hashCode 方法说明:

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.如果根据 equals(Object) 方法两个对象相等,则对两个对象中的每一个调用 hashCode 方法必须产生相同的整数结果。

Also, the hashCode method does not give the ASCII value of an object, merely an integer value that uniquely defines the object.此外, hashCode方法不提供对象的 ASCII 值,仅提供唯一定义对象的整数值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM