1. Introduce the hashCode method
-
hashCode()
The role of is to get the hash code, also known as hash code, it actually returns an int integer. The role of this hash code is to determine the index position of the object in the hash table. -
hashCode()
Defined in JDK's Object.java, which means that any class in Java containshashCode()
functions . -
The hash table stores key-value pairs, and its characteristic is that the corresponding "value" can be quickly retrieved according to the "key". This is where hash codes are used! (You can quickly find the desired object).
2. Why do you need the hashCode method?
In the process of writing programs, determining whether two objects are the same is a very common and often faced problem. The hashCode()
method is used to improve the speed of comparing two objects.
Let's take "How HashSet checks for duplicates" as an example to illustrate why there is a hashCode :
-
HashSet
When you add an object , HashSet will first calculate thehashcode
value of the object to determine where the object is added, and also compare it with thehashcode
values . If there is no matchhashcode
,HashSet
it will assume that the object does not appear repeatedly. -
But if objects with the same
hashcode
value , then the equals() method is called to checkhashcode
whether the equal objects are really the same. If the two are the same,HashSet
the join operation will not succeed. If it is different, it will be rehashed elsewhere. -
In this way, we greatly reduce the number
equals
of times, and accordingly greatly improve the execution speed.
3. What is the relationship between the hashCode() and equals() methods?
Java defines the equals() method and hashCode() method as follows:
- Multiple calls to the hashCode() method on the same object always return the same integer value.
- 如果 a.equals(b),则一定有 a.hashCode() 一定等于 b.hashCode()。
- 如果 !a.equals(b),则 a.hashCode() 不一定等于 b.hashCode()。此时如果 a.hashCode() 总是不等于 b.hashCode(),会提高 hashtables 的性能。
- a.hashCode()==b.hashCode() 则 a.equals(b) 可真可假
- a.hashCode()!= b.hashCode() 则 a.equals(b) 为假。
上面结论简记:
- 如果两个对象 equals,Java 运行时环境会认为他们的 hashCode 一定相等。
- 如果两个对象不 equals,他们的 hashCode 有可能相等。
- 如果两个对象 hashCode 相等,他们不一定 equals。
- 如果两个对象 hashCode 不相等,他们一定不 equals。
4、为什么重写 equals 方法必须重写 hashcode 方法 ?
-
我们上面讲解到 如果 两个对象
equals
的话,那么它们的hashCode
值必然相等。如果只重写了equals
方法,而不重写hashCode
的方法,会造成hashCode
的值不同,而equals
方法判断出来的结果为true
。 -
在Java中的一些容器中,不允许有两个完全相同的对象,插入的时候,如果判断相同则会进行覆盖。这时候如果只重写了
equals
的方法,而不重写hashCode
的方法,Object中hashCode
是根据对象的存储地址转换而形成的一个哈希值。这时候就有可能因为没有重写hashCode
方法,造成相同的对象散列到不同的位置而造成对象的不能覆盖的问题。
例如
Dog类
package com.xiao;
/**
* @author :小肖
* @date :Created in 2022/3/11 14:42
*/
public class Dog {
private String name;
private Integer age;
public Dog() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()){
return false;
}
Dog dog = (Dog) obj;
if(dog.getAge() == age && dog.getName().equals(name)){
return true;
}
return false;
}
}
复制代码
测试类
import com.xiao.Dog;
public class Test {
public static void main(String[] args) {
Dog dog = new Dog("小旺",2);
Dog dog1 = new Dog("小旺",2);
System.out.println("equals结果:" + dog.equals(dog1));
System.out.println("dog 的 hashCode 值是否等于 dog1 的 hashCode 值:" +(dog.hashCode() == dog1.hashCode()));
}
}
复制代码
测试结果
equals结果:true
dog 的 hashCode 值是否等于 dog1 的 hashCode 值:false
复制代码