当前位置:网站首页>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 !!!
边栏推荐
- This is a mathematical problem
- About XML file (VI) - the difference between JSON and XML file
- DDD 超越 MVC了吗
- While loop
- Ncnn allocator memory allocator
- 4. Some thoughts on asynctool framework
- After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle
- ncnn Allocator内存分配器
- Face key point detection
- 仿射变换实现
猜你喜欢

无法访问此网站无法找到DNS地址DNS_PROBE_STARTED怎么办?

OpenVINO中的FCOS人脸检测模型代码演示

多项式插值拟合(二)

5. Is the asynctool framework flawed?

After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle

BiSeNetV1 面部分割

RESNET learning notes

多项式插值拟合(一)
![[template record] string hash to judge palindrome string](/img/90/43109c6bd008990fa5b8147e629685.png)
[template record] string hash to judge palindrome string

Several methods of face detection
随机推荐
Bisenetv1 face segmentation
Face key point detection
JDBC connection to MySQL database
yolov5 ncnn 推理
Advanced usage of the responsibility chain pattern
Letv a plus de 400 employés? Le jour de l'immortel sans patron, les autorités ont répondu...
【单片机仿真】(二十)ORG — 设置起始地址
自动装配 & 集合注入
【单片机仿真】(十九)介绍汇编、汇编指令、伪指令
[MCU simulation] (IV) addressing mode register addressing and direct addressing
ncnn 线程
【单片机仿真】(十二)指令系统逻辑运算指令 — 逻辑异或指令XRL、累加器清0和取反指令
Ncnn mat matrix class
【单片机仿真】(十三)指令系统逻辑运算指令 — 移位指令
半年时间的思考
[Jianzhi offer] 31-35 questions (judge whether a sequence is one of the out of stack sequences, sequence print binary tree, branch print, and reverse print each line), judge whether the sequence is th
[MCU simulation] (XX) org - set start address
MySQL面试题(2022)
Specifications, multi table query basis
BiSeNetV1 面部分割