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 ？？
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.
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 , 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 .
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
hashCode() Reference article : https://blog.csdn.net/xusiwei1236/article/details/45152201