当前位置:网站首页>hashCode()与equals()之间的关系
hashCode()与equals()之间的关系
2022-07-06 09:20:00 【快醒醒鸭今天你编程了吗?】
在java中,每一个对象可以调用自己的hashcode()方法得到自己的哈希值
- 如果两个对象的hashcode不相同,那么这两个对象肯定是不同的两个对象
- 如果两个对象的hashcode相同,不代表这两个对象一定是同一个对象,也可能是两个对象
- 如果两个对象相等,那么他们的hashCode就一定相同
在java的一些集合类的实现类中,在比较两个对象是否相等的时候,会根据上面的原则,会先调用对象的hashCode()方法得到hashCode进行比较,如果hashCode不相同就可以直接认为这两个对象不相同,如果hashCode值相同,那么就会进一步调用equals()方法进行比较,而equals()方法,就是用来最终确定两个对象是不是相等,通常equals实现会比较重,逻辑比较多,而hashCode()主要就是得到一个哈希值,实际上就是一个数字,相对而言比较轻,所以在比较两个对象的时候,通常会先用hashCode
注意:我们重写了equals()方法,那么就要注意hashCode()方法,一定要保证能遵守上述规则。
如下图:
package com.ws;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<User,String> hashMap = new HashMap<>();
hashMap.put(new User("wangshun"),"123");
System.out.println(hashMap.get(new User("wangshun")));
}
}
class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName(){
return name;
}
@Override
public boolean equals(Object obj) {
User user = (User) obj;
return user.getName().equals(this.name);
}
}
没重写hashcode之前运行结果为null
package com.ws;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<User,String> hashMap = new HashMap<>();
hashMap.put(new User("wangshun"),"123");
System.out.println(hashMap.get(new User("wangshun")));
}
}
class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName(){
return name;
}
@Override
public boolean equals(Object obj) {
User user = (User) obj;
return user.getName().equals(this.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
}
重写hashcode之后为123
边栏推荐
- Set container
- 13 power map
- 7. Relationship between array, pointer and array
- TYUT太原理工大学2022数据库题库选择题总结
- Design a key value cache to save the results of the most recent Web server queries
- There is always one of the eight computer operations that you can't learn programming
- 系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
- arduino+水位传感器+led显示+蜂鸣器报警
- [中国近代史] 第五章测验
- 西安电子科技大学22学年上学期《基础实验》试题及答案
猜你喜欢
6.函数的递归
1.初识C语言(1)
Common method signatures and meanings of Iterable, collection and list
图书管理系统小练习
Introduction and use of redis
2. C language matrix multiplication
View UI plus released version 1.2.0 and added image, skeleton and typography components
MySQL Database Constraints
13 power map
Alibaba cloud microservices (III) sentinel open source flow control fuse degradation component
随机推荐
165. Compare version number - string
C语言实现扫雷游戏(完整版)
Data manipulation language (DML)
5. Download and use of MSDN
View UI plus released version 1.3.1 to enhance the experience of typescript
System design learning (I) design pastebin com (or Bit.ly)
C语言入门指南
First acquaintance with C language (Part 1)
Change vs theme and set background picture
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
FileInputStream和BufferedInputStream的比较
JS interview questions (I)
西安电子科技大学22学年上学期《基础实验》试题及答案
IPv6 experiment
4.分支语句和循环语句
There is always one of the eight computer operations that you can't learn programming
ROS machine voice
2.C语言初阶练习题(2)
【九阳神功】2018复旦大学应用统计真题+解析