当前位置:网站首页>Relationship between hashcode() and equals()
Relationship between hashcode() and equals()
2022-07-06 13:41:00 【Wake up duck, did you program today?】
stay java in , Each object can call its own hashcode() Method to get its own hash value
- If I have two objects hashcode inequality , Then these two objects must be different objects
- If I have two objects hashcode identical , It does not mean that these two objects must be the same object , It can also be two objects
- If two objects are equal , So their hashCode It must be the same
stay java In the implementation class of some collection classes , When comparing whether two objects are equal , According to the above principles , Will call the object first hashCode() Method to get hashCode Compare , If hashCode If you are different, you can directly think that the two objects are different , If hashCode Same value , Then it will call further equals() Methods for comparison , and equals() Method , It is used to finally determine whether two objects are equal , Usually equals Implementation will be heavy , More logic , and hashCode() The main thing is to get a hash value , It's actually a number , Relatively light , So when comparing two objects , Usually use it first hashCode
Be careful : We rewrote equals() Method , Then pay attention to hashCode() Method , Be sure to follow the above rules .
Here's the picture :
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);
}
}
No rewriting hashcode The previous running result is 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();
}
}
rewrite hashcode For after 123
边栏推荐
- String ABC = new string ("ABC"), how many objects are created
- (超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
- string
- Redis cache obsolescence strategy
- There is always one of the eight computer operations that you can't learn programming
- 【九阳神功】2021复旦大学应用统计真题+解析
- About the parental delegation mechanism and the process of class loading
- 8.C语言——位操作符与位移操作符
- 学编程的八大电脑操作,总有一款你不会
- 更改VS主题及设置背景图片
猜你喜欢
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
MPLS experiment
6. Function recursion
The latest tank battle 2022 - Notes on the whole development -2
FAQs and answers to the imitation Niuke technology blog project (III)
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
Redis的两种持久化机制RDB和AOF的原理和优缺点
西安电子科技大学22学年上学期《射频电路基础》试题及答案
5. Download and use of MSDN
Mortal immortal cultivation pointer-2
随机推荐
Floating point comparison, CMP, tabulation ideas
Mortal immortal cultivation pointer-1
[graduation season · advanced technology Er] goodbye, my student days
The difference between overloading and rewriting
3.猜数字游戏
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
PriorityQueue (large root heap / small root heap /topk problem)
Rich Shenzhen people and renting Shenzhen people
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
1.初识C语言(1)
Why use redis
8. C language - bit operator and displacement operator
IPv6 experiment
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
[中国近代史] 第六章测验
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
ABA问题遇到过吗,详细说以下,如何避免ABA问题
C language to achieve mine sweeping game (full version)
View UI plus released version 1.3.1 to enhance the experience of typescript
[the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis