当前位置:网站首页>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 .
边栏推荐
- mysql事务是什么
- [flask advanced] solve the classic error reporting of flask by combining the source code: working outside of application context
- Learn NLP with Transformer (Chapter 3)
- 企业实践开源的动机
- [information system project manager] thought map series essence summary
- HCIA experiment (09)
- Dataset and dataloader data loading
- BeautifulSoup的一些用法
- Last week's hot review (7.18-7.24)
- AI system frontier dynamics issue 43: ONEFLOW V0.8.0 officially released; GPU finds human brain connections; AI doctoral online crowdfunding research topic
猜你喜欢

Hcip experiment (02)

HCIP(13)
![[flask advanced] deeply understand the application context and request context of flask from the source code](/img/67/88c21967da28d09694568ebdaae44c.png)
[flask advanced] deeply understand the application context and request context of flask from the source code

MLX90640 红外热成像仪测温模块开发笔记(五)

NowCoderTOP7-11——持续更新ing

Code representation learning: introduction to codebert and other related models
Learn NLP with Transformer (Chapter 4)

2021 scenery written examination summary

mysql事务是什么

MySQL | GROUP_CONCAT函数,将某一列的值用逗号拼接
随机推荐
Openstack Skyline 组件安装
上周热点回顾(7.18-7.24)
接口流量突增,如何做好性能调优?
Guys, flick CDC table API, Mysql to MySQL, an application that can
MySQL | GROUP_CONCAT函数,将某一列的值用逗号拼接
Learn NLP with Transformer (Chapter 6)
[domain generalization] 2022 IJCAI domain generalization tutorial Report
100W!
What is MySQL transaction
代码的表示学习:CodeBERT及其他相关模型介绍
SQL语言(四)
SQL语言(二)
推荐系统-协同过滤在Spark中的实现
TPS calculation in performance test [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
Nb-iot control LCD (date setting and reading)
ESP8266 使用 DRV8833驱动板驱动N20电机
JS collection
Learn NLP with Transformer (Chapter 8)
txt转csv文件,隔行出现空行
Disabled and readonly and focus issues