Java重写equals及hashcode方法流程解析
作者:经典鸡翅 时间:2023-10-14 06:53:13
初步探索
首先我们要了解equals方法是什么,hashcode方法是什么。
equals方法
equals 是java的obejct类的一个方法,equals的源码如下:
public boolean equals(Object paramObject){
return(this == paramObject);
}
由此我们可以看到equals是用来比较两个对象的内存地址是否相等。
hashCode方法
hashCode方法是本地方法,用于计算出对象的一个散列值,用于判断在集合中对象是否重复的关键。
一条定理
equals相同的对象,hashCode必然相同。
代码示例
建立一个Student类。
public class Student {
private String name;
private int age;
private String QQ;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name) &&
Objects.equals(QQ, student.QQ);
}
}
在 student 类中,我们重写了equals方法。
书写一个测试类
public class Test {
public static void main(String[] args) {
Student student = new Student();
Student student2 = new Student();
System.out.println(student.equals(student2)); //true
System.out.println(student.hashCode()); //356573597
System.out.println(student2.hashCode()); //1735600054
HashMap<Student, String> map = new HashMap<>();
map.put(student,"123");
map.put(student2,"456");
System.out.println(map.get(student));
System.out.println(map.get(student2));
}
}
输出
true
356573597 student 的hashcode值
1735600054 student 2的hashcode值
123
456
此时,我们发现 equals 相等的对象,hashcode却不相等,同时在map中用不同的对象进行了存储,map计算出来的hash值不同,但equals却相同。这时候懵了。到底两个对象一样不一样呢。
所以我们在重写equals的时候,必须重写hashcode。
重新定义 student 类
public class Student {
private String name;
private int age;
private String QQ;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name) &&
Objects.equals(QQ, student.QQ);
}
@Override
public int hashCode() {
return Objects.hash(name, age, QQ);
}
}
再次测试
public class Test {
public static void main(String[] args) {
Student student = new Student();
Student student2 = new Student();
System.out.println(student.equals(student2)); //true
System.out.println(student.hashCode()); // 29791
System.out.println(student2.hashCode()); // 29791
HashMap<Student, String> map = new HashMap<>();
map.put(student,"123");
map.put(student2,"456");
System.out.println(map.get(student)); //456
System.out.println(map.get(student2)); //456
}
}
最后的输出
true
29791 //相同的对象
29791
456 //说明以一个值key存储,相同的值
456
几条定理
1、两个对象的equals相等,hashcode必然相等。
2、两个对象不等,hashcode也可能相等。
3、hashcode相等,对象不一定相等。
4、hashcode不等,对象一定不等。
来源:https://www.cnblogs.com/jichi/p/12593866.html
标签:Java,重写,equals,hashcode
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微信公众平台(测试接口)准备工作
2023-05-24 15:51:06
![](https://img.aspxhome.com/file/2023/8/132218_0s.png)
Java利用endorsed如何覆盖jdk提供的类详解
2021-05-30 08:26:33
![](https://img.aspxhome.com/file/2023/6/129136_0s.png)
c#日期间隔计算示例
2022-11-28 07:49:48
c#实现万年历示例分享 万年历农历查询
2022-11-08 23:26:57
C#实现导入CSV文件到Excel工作簿的方法
2022-09-13 12:24:02
java设计模式--三种工厂模式详解
2023-11-29 09:54:06
![](https://img.aspxhome.com/file/2023/7/60337_0s.png)
关于feign.codec.DecodeException异常的解决方案
2022-01-28 15:50:19
![](https://img.aspxhome.com/file/2023/7/118897_0s.png)
C#与C++之间类型的对应知识点总结
2023-09-07 21:02:56
c#的dllimport使用方法详解
2023-04-20 04:01:49
Java 十大排序算法之希尔排序刨析
2021-11-16 09:27:31
![](https://img.aspxhome.com/file/2023/7/68137_0s.png)
Kotlin 封装万能SharedPreferences存取任何类型详解
2021-11-16 06:16:46
SpringBoot详解如何进行整合Druid数据源
2022-05-10 10:46:54
![](https://img.aspxhome.com/file/2023/0/106080_0s.png)
详解SpringBoot获得Maven-pom中版本号和编译时间戳
2022-11-27 19:45:27
![](https://img.aspxhome.com/file/2023/6/91476_0s.png)
Java按照List内存储的对象的某个字段进行排序的实例
2023-12-11 11:58:35
Android仿支付宝中余额宝的数字动画效果
2021-10-17 08:32:54
![](https://img.aspxhome.com/file/2023/1/137721_0s.gif)
详解Spring Boot Profiles 配置和使用
2021-10-05 22:54:57
Java从同步容器到并发容器的操作过程
2021-10-14 05:26:58
![](https://img.aspxhome.com/file/2023/0/65640_0s.png)
spring webflux自定义netty 参数解析
2023-07-26 18:38:25
![](https://img.aspxhome.com/file/2023/1/57811_0s.jpg)
Java实现将PDF转为图片格式的方法详解
2022-08-21 07:03:36
![](https://img.aspxhome.com/file/2023/8/122308_0s.jpg)
20个非常实用的Java程序代码片段
2022-02-08 10:35:44