当前位置:网站首页>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 difference between overloading and rewriting
- [hand tearing code] single case mode and producer / consumer mode
- 学编程的八大电脑操作,总有一款你不会
- Rich Shenzhen people and renting Shenzhen people
- 3. Number guessing game
- Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
- 6. Function recursion
- 【九阳神功】2016复旦大学应用统计真题+解析
- FAQs and answers to the imitation Niuke technology blog project (II)
- [面试时]——我如何讲清楚TCP实现可靠传输的机制
猜你喜欢

20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)

编写程序,模拟现实生活中的交通信号灯。

Mortal immortal cultivation pointer-1

5. Function recursion exercise

Cookie和Session的区别

C language Getting Started Guide

Leetcode. 3. Longest substring without repeated characters - more than 100% solution

FAQs and answers to the imitation Niuke technology blog project (I)

Service ability of Hongmeng harmonyos learning notes to realize cross end communication

MPLS experiment
随机推荐
西安电子科技大学22学年上学期《信号与系统》试题及答案
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
Detailed explanation of redis' distributed lock principle
C语言入门指南
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
【九阳神功】2017复旦大学应用统计真题+解析
There is always one of the eight computer operations that you can't learn programming
string
4.分支语句和循环语句
C language Getting Started Guide
MATLAB打开.m文件乱码解决办法
【九阳神功】2018复旦大学应用统计真题+解析
hashCode()与equals()之间的关系
vector
Mortal immortal cultivation pointer-1
[面试时]——我如何讲清楚TCP实现可靠传输的机制
PriorityQueue (large root heap / small root heap /topk problem)
Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
Floating point comparison, CMP, tabulation ideas
深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning