当前位置:网站首页>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
边栏推荐
- fianl、finally、finalize三者的区别
- MySQL中count(*)的实现方式
- [the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
- Leetcode.3 无重复字符的最长子串——超过100%的解法
- 2.初识C语言(2)
- Redis的两种持久化机制RDB和AOF的原理和优缺点
- Rich Shenzhen people and renting Shenzhen people
- [面試時]——我如何講清楚TCP實現可靠傳輸的機制
- Set container
- [中国近代史] 第六章测验
猜你喜欢
西安电子科技大学22学年上学期《信号与系统》试题及答案
arduino+水位传感器+led显示+蜂鸣器报警
【手撕代码】单例模式及生产者/消费者模式
The latest tank battle 2022 full development notes-1
9.指针(上)
7. Relationship between array, pointer and array
A piece of music composed by buzzer (Chengdu)
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
3. Number guessing game
1.初识C语言(1)
随机推荐
5. Download and use of MSDN
[the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
7. Relationship between array, pointer and array
C language Getting Started Guide
ArrayList的自动扩容机制实现原理
这次,彻底搞清楚MySQL索引
自定义RPC项目——常见问题及详解(注册中心)
编写程序,模拟现实生活中的交通信号灯。
About the parental delegation mechanism and the process of class loading
Arduino+ water level sensor +led display + buzzer alarm
C language Getting Started Guide
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
2. C language matrix multiplication
4.二分查找
Custom RPC project - frequently asked questions and explanations (Registration Center)
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
Mortal immortal cultivation pointer-2
抽象类和接口的区别
Zatan 0516
8. C language - bit operator and displacement operator