当前位置:网站首页>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 !!!
边栏推荐
- [MCU simulation] (IX) instruction system - add, ADDC, sub, Inc, Dec, Da of arithmetic operation instructions
- 需要慢一点点
- Is there really no way out for functional testing? 10K capping is never a joke
- Specifications, multi table query basis
- 审视自己投资的路
- ncnn DataReader&Extractor&blob
- Polynomial interpolation fitting (I)
- Redis和其他数据库的比较
- Data source object management (third-party object resources) & load properties file
- 多锻炼身体有好处
猜你喜欢

DDD 超越 MVC了吗

After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle
![[regression prediction] lithium ion battery life prediction based on particle filter with matlab code](/img/1d/5562934499edfb4781b1118d0c4f9c.png)
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code

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

Redis和其他数据库的比较

Systick timer basic learning and hand tearing code

04_服务注册Eureka

JDBC连接Mysql数据库

A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?

05_ Service call ribbon
随机推荐
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code
多表查询——案例练习
【剑指Offer】31-35题(判断一个序列是否是栈的出栈序列之一,层序打印二叉树以及分行打印、每行逆着打印),判断序列是否是二叉搜索树的后序遍历路径,二叉树找一条权值为K的路径,复制复杂链表
[face recognition] face recognition based on histogram histogram with matlab code
Code demonstration of fcos face detection model in openvino
【单片机仿真】(十九)介绍汇编、汇编指令、伪指令
First knowledge of JPA (ORM idea, basic operation of JPA)
LETV has more than 400 employees? Living a fairy life without a boss, the official responded
功能测试真的没有出路了吗?10k封顶从来不是开玩笑的.....
OpenVINO中的FCOS人脸检测模型代码演示
BiSeNetV1 面部分割
D. Permutation Restoration(贪心/双指针/set)
04_服务注册Eureka
【回归预测】基于粒子滤波实现锂离子电池寿命预测附matlab代码
Obvious things
SysTick定时器的基础学习以及手撕代码
【单片机仿真】(二十一)DB(Define Byte)— 定义字节
ResNet学习笔记
While loop
深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之SMOTE采样法及Borderline-SMOTE采样法]