The hashcode() method that must be mastered, 해시게임.
1. What is hashcode?
1. What are hash and hash tables?
If you want to know this hashcode, you must first know the hash,
and look at it through Baidu Encyclopedia:
Hash is a function, and the implementation in this function is an algorithm,
which is to obtain a hash value through a series of algorithms.
At this time, we need to know another thing, the hash table,
the hash value obtained by the hash algorithm is in this hash table,
that is to say, the hash table is composed of all the hash values,
there are many kinds of hash functions,
also, It means that there are many algorithms to get the hash value,
such as the three in the screenshot above, and we will take the first one later.
With the previous foundation, the explanation here is simple.
The hashcode is obtained through the hash function.
In layman’s terms, it is obtained through a certain algorithm.
The hashcode has a corresponding position in the hash table.
Every object has a hashcode, how do you get the hashcode of the object?
First of all, an object must have a physical address.
In other blog posts, the hashcode will be said to represent the address of the object.
This will definitely make readers from misunderstanding.
The physical address of the object is different from the hashcode address.
The hashcode represents the address of the object.
The location of the object in the hash table,
and the physical address of the object are stored in the memory address,
so how does the object get the hashcode?
Convert the internal address (that is, the physical address) of the object into an integer,
and then the integer gets the hashcode through the algorithm of the hash function.
So, what is hashcode? It is the corresponding position in the hash table.
If it is not clear here, for example, there are eight positions in the hash table, such as hashcode 1, hashcode 2, (…) 3, 4, 5, 6, 7, 8, there is an object A,
A’s The physical address is converted to an integer 17 (this is if), through the direct remainder algorithm, 17%8=1, then the hashcode of A is 1, and A is in the position of 1 in the hash table.
There will definitely be other questions, then look at the following,
here is just an example to let you know what is the meaning of hashcode.
2. What is the role of the hashcode?
I have said so much about the hash function, how the hashcode is obtained,
and the hashcode corresponds to the location in the hash table.
You may have questions, as to why the hashcode does not directly write the physical address,
and whether you need to use another one. hash table to represent the address of the object?
Next, I will tell you the role of the hashcode.
1. The existence of HashCode is mainly for the convenience of search.
HashCode is used to determine the storage address of the object in the hash storage structure (in the second half of the sentence, the use of hashcode to represent the object is the location in the hash table)
Why does hashcode search faster? For example,
we have a memory that can store 1,000 numbers,
and we need to store 1,000 different numbers in it.
The most stupid way is to store a number and traverse it again.
See if there are the same numbers.
When 900 numbers are stored, and 901 numbers are stored,
it needs to be compared with 900 numbers,
which is very troublesome and time-consuming.
Use hashcode to record the location of the object, come take a look.
There are 1, 2, 3, 4, 5, 6, 7, and 8 positions in the hash table,
the first number is stored, the hashcode is 1, and the number is placed in the position of 1 in the hash table,
and 100 numbers are stored.
There are many numbers in 8 positions in the hash table.
There may be 20 numbers in 1.
When storing 101 numbers, he first checks the position corresponding to the hashcode value.
If it is 1, then there are 20 numbers that are the same as his hashcode. ,
he only needs to compare (equals) with these 20 numbers. If each one is the same,
then put it in the position of 1,
so that the number of comparisons is much less.
In fact, there are many positions in the hash table.
Here is just an example. 8, so the number of comparisons will make you feel quite a lot. In fact,
if the hash table is large, the number of comparisons will be very small.
By comparing the original method and the method using hashcode, we know the role of hashcode and why we should use the hashcode
3. The relationship between equals method and hashcode?
From the previous example, you can probably know that the hashcode is used to compare first.
If the hashcode is equal, then the equals method is used to compare whether the two objects are equal.
Use an example to illustrate: the 8 positions in the hash table mentioned above are like 8 buckets.
Each bucket can hold a lot of objects.
Object A is obtained through the hash function algorithm and put into bucket No. 1,
of course, sure Other objects will also be placed in bucket 1.
If object B is also assigned to bucket 1 through an algorithm,
how does it identify whether other objects in the bucket are the same as it?
At this time, the equals method is needed to filter.
1. If the equals of two objects are equal, then the HashCode of the two objects must also be the same
2. If the HashCode of two objects is the same,
it does not mean that the two objects are the same.
It only means that the two objects are stored in the same location in the hash storage structure.