为什么重写 equals 时必须重写 hashCode 方法?(Why do you have to override the hashcode method when overriding equals?)

hashCode()与 equals()

面试官可能会问你:“你重写过  和 么,为什么重写  时必须重写  方法?”

hashcode
equals
equals
hashCode

1)hashCode()介绍:

 的作用是获取哈希码,也称为散列码;它实际上是返回一个 int 整数。这个哈希码的作用是确定该对象在哈希表中的索引位置。定义在 JDK 的  类中,这就意味着 Java 中的任何类都包含有  函数。另外需要注意的是:  的 hashcode 方法是本地方法,也就是用 c 语言或 c++ 实现的,该方法通常用来将对象的 内存地址 转换为整数之后返回。

hashCode()
hashCode()
Object
hashCode()
Object
public native int hashCode();

散列表存储的是键值对(key-value),它的特点是:能根据“键”快速的检索出对应的“值”。这其中就利用到了散列码!(可以快速找到所需要的对象)

2)为什么要有 hashCode?

我们以“ 如何检查重复”为例子来说明为什么要有 hashCode?

HashSet

当你把对象加入  时, 会先计算对象的 hashcode 值来判断对象加入的位置,同时也会与其他已经加入的对象的 hashcode 值作比较,如果没有相符的 hashcode, 会假设对象没有重复出现。但是如果发现有相同 hashcode 值的对象,这时会调用 equals()方法来检查 hashcode 相等的对象是否真的相同。如果两者相同, 就不会让其加入操作成功。如果不同的话,就会重新散列到其他位置。(摘自我的 Java 启蒙书《Head fist java》第二版)。这样我们就大大减少了 equals 的次数,相应就大大提高了执行速度。

HashSet
HashSet
HashSet
HashSet

3)为什么重写  时必须重写  方法?

equals
hashCode

如果两个对象相等,则 hashcode 一定也是相同的。两个对象相等,对两个对象分别调用 equals 方法都返回 true。但是,两个对象有相同的 hashcode 值,它们也不一定是相等的 。因此,equals 方法被覆盖过,则  方法也必须被覆盖。

hashCode

的默认行为是对堆上的对象产生独特值。如果没有重写 ,则该 class 的两个对象无论如何都不会相等(即使这两个对象指向相同的数据)

的默认行为是对堆上的对象产生独特值。如果没有重写 ,则该 class 的两个对象无论如何都不会相等(即使这两个对象指向相同的数据)

hashCode()
hashCode()

4)为什么两个对象有相同的 hashcode 值,它们也不一定是相等的?

在这里解释一位小伙伴的问题。以下内容摘自《Head Fisrt Java》。

因为  所使用的杂凑算法也许刚好会让多个对象传回相同的杂凑值。越糟糕的杂凑算法越容易碰撞,但这也与数据值域分布的特性有关(所谓碰撞也就是指的是不同的对象得到相同的 。

hashCode()
hashCode

我们刚刚也提到了 ,如果  在对比的时候,同样的 hashcode 有多个对象,它会使用  来判断是否真的相同。也就是说  只是用来缩小查找成本。

HashSet
HashSet
equals()
hashcode

更多关于  和  的内容可以查看:Java hashCode() 和 equals()的若干问题解答

hashcode()
equals()
————————

hashCode()与 equals()

The interviewer may ask you, “have you rewritten , and , and why do you have to rewrite , when rewriting , method?”

hashcode
equals
equals
hashCode

1) Introduction to hashcode():

The function of is to obtain hash code, also known as hash code; It actually returns an int integer. This hash code is used to determine the index position of the object in the hash table. Defined in the JDK’s {class, which means that any class in java contains a} function. In addition, it should be noted that the hashcode method of is a local method, that is, it is implemented in C language or C + +. This method is usually used to convert the memory address of the object into an integer and return it.

hashCode()
hashCode()
Object
hashCode()
Object
public native int hashCode();

Hash table stores key value pairs. Its feature is that it can quickly retrieve the corresponding “value” according to the “key”. This makes use of hash code! (you can quickly find the required object)

2) Why hashcode?

Let’s take “how to check for duplicates” as an example to illustrate why hashcode is required?

HashSet

When you add an object to {, you will first calculate the hashcode value of the object to judge the location of the object. At the same time, it will also be compared with the hashcode values of other added objects. If there is no matching hashcode, it will be assumed that the object does not appear repeatedly. However, if an object with the same hashcode value is found, the equals () method will be called to check whether the objects with the same hashcode are really the same. If they are the same, the join operation will not be successful. If it is different, it will be hashed to another location. (from my java enlightenment book head fist Java, Second Edition). In this way, we greatly reduce the number of equals and greatly improve the execution speed.

HashSet
HashSet
HashSet
HashSet

3) Why do I have to override the {method when overriding}?

equals
hashCode

If two objects are equal, the hashcode must be the same. If the two objects are equal, calling the equals method on the two objects returns true. However, two objects have the same hashcode value, and they are not necessarily equal. Therefore, if the equals method is overridden, the equals method must also be overridden.

hashCode

The default behavior of is to produce unique values for objects on the heap. If it is not overridden, the two objects of the class will not be equal in any case (even if they point to the same data)

The default behavior of is to produce unique values for objects on the heap. If it is not overridden, the two objects of the class will not be equal in any case (even if they point to the same data)

hashCode()
hashCode()

4) Why do two objects have the same hashcode value and they are not necessarily equal?

Explain here the problem of a little partner. The following is taken from head fisrt Java.

Because the hash algorithm used may just cause multiple objects to return the same hash value. The worse the hash algorithm is, the easier it is to collide, but this is also related to the characteristics of data value range distribution (the so-called collision means that different objects get the same result).

hashCode()
hashCode

We just mentioned that if the same hashcode has multiple objects during comparison, it will use the  to judge whether it is really the same. In other words, it is only used to reduce the search cost.

HashSet
HashSet
equals()
hashcode

For more information about Java hashcode() and equals(), please see the answers to some questions about Java hashcode() and equals()

hashcode()
equals()