当前位置:网站首页>Why should the hashcode () method be rewritten when rewriting the equals () method
Why should the hashcode () method be rewritten when rewriting the equals () method
2022-07-25 11:17:00 【Fried tomatoes with duck eggs】
First of all, we need to know one thing , For two different objects , They use hashcode() The value calculated by the method may be the same , We can't use hashcode( ) To determine if two objects are equal , Must pass equals() Method .
Two objects equals() equal ,hashcode() It must be equal
Two objects hashcode() Unequal ,equals() It must be different
Two objects hashcode() equal , Objects are not necessarily equal , Need to pass through equals() Further judgment .)
Why should we rewrite equals() Methods? ?
Sometimes, when we judge objects in our business system, we sometimes need not a strict equality , It's a business where objects are equal . under these circumstances , Native equals Methods can not meet our needs .
What we know JavaBean Superclass of ( Parent class ) yes Object class ,JavaBean Medium equals The method is inherited from Object The method in .Object Class equals() Method is used to compare whether the memory addresses of the objects pointed to by two references are consistent . It is not to compare whether the attribute values of two objects are consistent , So at this point we need to rewrite equals() Method .
Let me put it in a simple way , There's a student class id,name, then A.id=1;A.name=“ Zhang San ”, There's one at this time B.id Also equal to 1,B.name=“ Zhang San ”, Use at this time equals Method to calculate, we get the result is false, But our business needs just want to compare literally , What you want is true, At this time, we have to rewrite it equals() Method , When we rewrite equals() Why rewrite after method hashcode() Methods? ?
We stipulated equals In the case of equality ,hashCode() Must be equal , Another official explanation is that we are writing a class to rewrite equals Method is not overridden hashcode When the method is used , There will be a situation :equals When the comparison results are equal ,hashcode It's not equal , Although there is no problem in this class , But if we put it in a hash set , In the hash set, a equals Equal and hashcode Different situations , This causes the hash set to equals Wrong judgment ( Because it passes first hashcode Comparative ,hashcode Different people will think it is different ), So in order to match the comparison in the hash set , We need to rewrite equals Method when overridden hashcode Method .
边栏推荐
- Reinforcement Learning 强化学习(三)
- Digital twin everything can be seen | connecting the real world and digital space
- NowCoderTOP1-6——持续更新ing
- 信息熵的定义
- 学习路之PHP--Phpstudy 提示 Mysqld.Exe: Error While Setting Value ‘NO_ENGINE_SUBSTITUTION 错误的解决办法
- Learn NLP with Transformer (Chapter 8)
- [树] 100. 相同的树
- mysql高级语句(一)(总有一个人的出现,让你的生活不再继续糟糕)
- HDD Hangzhou station full experience
- TPS calculation in performance test [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
猜你喜欢
信息熵的定义

Flask框架——Flask-WTF表单:数据验证、CSRF保护
Learn NLP with Transformer (Chapter 2)

NB-IOT控制液晶屏(日期的设置与读取)

From the perspective of open source, analyze the architecture design of SAP classic ERP that will not change in 30 years

【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源

2021 scenery written examination summary

Some usages of beautifulsoup

100W了!

Learn NLP with Transformer (Chapter 1)
随机推荐
Software Testing Technology: cross platform mobile UI automated testing (Part 1)
HCIA experiment (06)
txt转csv文件,隔行出现空行
[递归] 938. 二叉搜索树的范围和
二合一的集度,任重道远
Google Earth engine -- Statistics on the frequency of land classification year by year
Shell 脚本参数传递时有 \r 换行符问题
Learn NLP with Transformer (Chapter 8)
Esp8266 uses drv8833 drive board to drive N20 motor
[flask advanced] solve the classic error reporting of flask by combining the source code: working outside of application context
HDD Hangzhou station full experience
推荐系统-协同过滤在Spark中的实现
学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
Flask框架——Session与Cookie
学习周刊 - 总第 63 期 - 一款开源的本地代码片段管理工具
Learn NLP with Transformer (Chapter 7)
Last week's hot review (7.18-7.24)
基于cornerstone.js的dicom医学影像查看浏览功能
Google Earth Engine——统计逐年土地分类的频率
PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying