当前位置:网站首页>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
- Why use redis
- 1. C language matrix addition and subtraction method
- canvas基础2 - arc - 画弧线
- [during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
- 重载和重写的区别
- 3. Number guessing game
- [the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
- Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
- 一段用蜂鸣器编的音乐(成都)
猜你喜欢

Mortal immortal cultivation pointer-1

5. Download and use of MSDN

优先队列PriorityQueue (大根堆/小根堆/TopK问题)

C语言入门指南

(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)

.Xmind文件如何上传金山文档共享在线编辑?

2.C语言初阶练习题(2)

关于双亲委派机制和类加载的过程

5.MSDN的下载和使用

A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
随机推荐
String abc = new String(“abc“),到底创建了几个对象
【九阳神功】2021复旦大学应用统计真题+解析
自定义RPC项目——常见问题及详解(注册中心)
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
Cookie和Session的区别
[the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis
为什么要使用Redis
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
仿牛客技术博客项目常见问题及解答(三)
Wei Pai: the product is applauded, but why is the sales volume still frustrated
The latest tank battle 2022 full development notes-1
[the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
最新坦克大战2022-全程开发笔记-2
CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
3. Number guessing game
ABA问题遇到过吗,详细说以下,如何避免ABA问题
MySQL lock summary (comprehensive and concise + graphic explanation)
一段用蜂鸣器编的音乐(成都)
MySQL中count(*)的实现方式
A piece of music composed by buzzer (Chengdu)