当前位置:网站首页>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
边栏推荐
- [中国近代史] 第六章测验
- [the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
- 4.二分查找
- 透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
- 重载和重写的区别
- (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 language Getting Started Guide
- 【九阳神功】2018复旦大学应用统计真题+解析
- Arduino+ water level sensor +led display + buzzer alarm
- 4. Binary search
猜你喜欢
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
强化学习系列(一):基本原理和概念
C language Getting Started Guide
View UI plus released version 1.3.0, adding space and $imagepreview components
Cookie和Session的区别
5. Function recursion exercise
9. Pointer (upper)
随机推荐
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
[the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
Detailed explanation of redis' distributed lock principle
杂谈0516
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
用栈实现队列
This time, thoroughly understand the MySQL index
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
[modern Chinese history] Chapter 6 test
[中国近代史] 第九章测验
C language to achieve mine sweeping game (full version)
简述xhr -xhr的基本使用
View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
3.猜数字游戏
(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
Write a program to simulate the traffic lights in real life.
仿牛客技术博客项目常见问题及解答(三)
List set map queue deque stack