当前位置:网站首页>Rewrite equals why rewrite hashcode
Rewrite equals why rewrite hashcode
2022-07-19 03:12:00 【51CTO】
rewrite equals Why rewrite hashcode?
Premise
Let's get to know object class
example
Suppose in business , We think that the same name is the same person
guess
If you do not override hashcode What's going to happen
Let's see below. map Of put What happened to the operation ?
We find that when there is conflict , The order of judgment is as follows
- Compare first key Of hash Whether the values are equal , Compare again key Whether the memory addresses of are equal perhaps key Of equals Whether it is equal or not
- hash It's not equal , Compare it again TreeNode type
- Whether there are equal nodes in the linked list
Because we think that the same name is the same person
When put When , Compare first student1 and student2 Of hash value , because student1 and student2 It's a different object , their hashcode Method is called object.hashcode Method , It compares the address of the object , Obviously , It is calculated by the following method hash It's not equal
hash Value inequality , Neither TreeNode type , Add to linked list
There's a contradiction
But now the name is the same , But in map But there are two people , It's obviously against our original intention
Obviously , The reason for the contradiction is to call object.hashcode Method , Calculate the unequal hash value , If hash The values are equal , Can meet the following if sentence
So , I want to design something with the same name student Produce the same hash value
rewrite hashcode
We know that different values may produce the same hash value , But in map This problem does not exist , Because when hash When the values are equal , And compare it equals Method , But we should also try to avoid hash Collision , After all hash Collision still has a little impact on performance
Sum up , rewrite equals To rewrite hashcode Just to avoid being like map In such a data structure , Prohibit the same key There are multiple value value .
end !!!
边栏推荐
- Multi table query - case exercise
- Several methods of face detection
- About XML file (VI) - the difference between JSON and XML file
- 【单片机仿真】(二十)ORG — 设置起始地址
- Pytorch best practices and code templates
- 多表查询——案例练习
- MySQL multi table query
- Advanced usage of the responsibility chain pattern
- 【单片机仿真】(五)寻址方式 — 立即寻址与寄存器间接寻址
- 快照:数据快照(数据兜底方式)
猜你喜欢

自动装配 & 集合注入

zsh: command not found: mysql

Polynomial interpolation fitting (III)

快照:数据快照(数据兜底方式)

About XML file (VI) - the difference between JSON and XML file

3. Asynctool framework principle source code analysis

ncnn param文件及bin模型可视化解析

Affine transformation implementation

yolov5 ncnn 推理

04_ Service registration Eureka
随机推荐
From the perspective of MySQL architecture, how does an SQL statement execute?
【剑指Offer】31-35题(判断一个序列是否是栈的出栈序列之一,层序打印二叉树以及分行打印、每行逆着打印),判断序列是否是二叉搜索树的后序遍历路径,二叉树找一条权值为K的路径,复制复杂链表
【单片机仿真】(二十一)DB(Define Byte)— 定义字节
MySQL replication table
【单片机仿真】(十七)控制转移类指令 — 调用及返回指令
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code
JDBC connection to MySQL database
Polynomial interpolation fitting (III)
4. Some thoughts on asynctool framework
多锻炼身体有好处
[MCU simulation] (XVII) control transfer instructions - call and return instructions
Tools and methods - Excel plug-in xltools
JPA初识(ORM思想、JPA的基本操作)
工具及方法 - Excel插件XLTools
[MCU simulation] (XXI) dB (define byte) - define byte
GraphQL初识
While loop
无线用的鉴权代码
Fiddler grabbing
5. Is the asynctool framework flawed?