Why must hashcode() be overridden when overriding the equals() method? | HashMap

Yard farmland 2020-11-11 09:01:29
hashcode overridden overriding equals method

WeChat search 「 Yard farmland, Xiao Qi 」, Focus on this program in New York , reply 「01-05」 You can get a selection of computer books 、 Personal writing notes 、 Big factory surface 、 Interview materials and other resources , mua ~

First of all, we have a hypothesis : Any two object Of hashCode It's all different .

So under this condition , There are two object They are equal. , So if you don't rewrite hashCode(), The calculated hash values are all different , It's going to be different buckets 了 , Lost in the sea of people , No more recognition , Just like equals() The conditions are contradictory , Certificate completion .

And the flower ~~

Now let's explore these two methods :

Actually hashCode() and equals() The methods are all in Object class That's what our ancestors defined ,Object It's all Java Medium class The originator of , By default , I can't get rid of it .

Since it's for nothing , Let's see what's in the big bag first , Google Object Of Oracle file :

So these methods can be used directly ~

go back to hashCode() and equals(), So if this new class No rewriting in (override) These two methods , Is the default inheritance Object class The definition in .

Let's go in and have a look equals() How to define it :

Note taking :

equals() The way is to compare the two references Whether it points to the same object.

Um. ??? Are you making fun of me? ?? Isn't it with == The same ??

Add :
One of the symbols we use to compare sizes ==
If it is primitive type, that == Is to compare the values ;
If it is reference type, So the comparison is between the two reference Whether it points to the same object.

Replenish :
Java There are two types of data :
Primitive type Yes and no 8 Kind of :byte, short, int, long, float, double, char, boolean.
Others are Reference type.
So although Java claim “Everything is object”, But there are still problems object Existence of data type .

I don't believe it , I will go to the source code to see how it is implemented .

Ha , It's true , I've been around for so long ,equals() Just use == To achieve !

Then why do you come up with such a method ?

answer : For you override~

For example, generally speaking, we want to compare the contents of these two strings , that :

str1 = “tianxiaoqi”;
str2 =  new String(“tianxiaoqi”);

str1 == str2; // return false
str1.equals(str2); // return true 

because String It's rewritten equals() Methodical :

Your ancestors left you for your own use , If you don't have to , The family also provided a default method , That's enough .

Okay , Let's see it again hashCode() Introduction to :

As for hashCode() What is it that returns , Not very relevant to this article , Interested students can see the reference This article [1], The conclusion is that :

What is returned is not necessarily the object's ( fictitious ) Memory address , Depending on the runtime library and JVM The concrete realization of .

But no matter how , You need to follow the conventions on the document , That is to say, for different object Returns the Unique hash value .

So ,

hashCode() To determine the key The number in this bucket , That's in the array index;

equals() To compare two object Same or not .

If you like this article , Remember to leave me a like message ~ Your support and recognition , It's the biggest driving force of my creation , See you in the next article !

This is Qi , New York program , Lifelong learners , Every night 9 spot , Let's meet you in the cloud study room !

See my... For more dry articles Github: https://github.com/xiaoqi6666/NYCSDE

Reference material


hashCode() Reference article : https://blog.csdn.net/xusiwei1236/article/details/45152201

本文为[Yard farmland]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云