当前位置:网站首页>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

边栏推荐
- Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
- TYUT太原理工大学2022数据库大题之E-R图转关系模式
- 6.函数的递归
- (ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
- C语言实现扫雷游戏(完整版)
- 2.C语言矩阵乘法
- 1.C语言矩阵加减法
- MySQL Database Constraints
- Inheritance and polymorphism (Part 2)
- 3.猜数字游戏
猜你喜欢

View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件

1. C language matrix addition and subtraction method

Database operation of tyut Taiyuan University of technology 2022 database

2.初识C语言(2)

(超详细二)onenet数据可视化详解,如何用截取数据流绘图

4.二分查找

Redis介绍与使用

抽象类和接口

Alibaba cloud microservices (I) service registry Nacos, rest template and feign client

Interview Essentials: talk about the various implementations of distributed locks!
随机推荐
[while your roommate plays games, let's see a problem]
【九阳神功】2020复旦大学应用统计真题+解析
12 excel charts and arrays
4. Binary search
1.C语言初阶练习题(1)
【九阳神功】2019复旦大学应用统计真题+解析
TYUT太原理工大学2022数据库大题之概念模型设计
7. Relationship between array, pointer and array
Iterable、Collection、List 的常见方法签名以及含义
3.输入和输出函数(printf、scanf、getchar和putchar)
CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
分支语句和循环语句
系统设计学习(一)Design Pastebin.com (or Bit.ly)
C语言入门指南
MySQL Database Constraints
Rich Shenzhen people and renting Shenzhen people
【九阳神功】2018复旦大学应用统计真题+解析
C语言入门指南
Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology