One.
What exactly is the way in which the hash table determines if the elements are the same? We're going to explore.
Just now we have AB in the table when we store AB. When you store AB again, you first put the algorithm into the calculation address. At the end of the day, this address is 5, then go to the table and find out that there are already elements in the 5 position. And then you're not going to save it? Not necessarily, we can only say that there are already elements in this position, there is no guarantee that this element is AB. I have two elements of the address is 5, not to judge that this element is the same.
For example, AB and BA, these two strings are not the same, the results obtained by hashing algorithm are consistent. They are in the same position, but the content is not the same.
In a hash table, there must be no duplication, that involves a problem, how does a hash table determine if the element is duplicated? The results of the hashing algorithm are not completely accurate. Look at the value first, then judge.
Not that the hash is worth the same result, that is the same element, after all, is to lend a process, through this process to get the same, and directly determine the same is different.
If the hashes are not the same, then there is no second-step judgment.
Now let's talk about the special things in the hash table, when the hashes are the same and the content is different, then it needs to be stored, which is the hash conflict (hash value). Two objects, but the hash value is not the same.
This is not the case because the general hashing algorithm is more complex.
This is the case, the solution is not unique, such as postponed. What if we postpone to the back and have no position? Extend the length of the array, or concatenate.
The concatenation is more interesting, the position is 5, hang one out as shown. This concatenation it will continue to calculate its position, is based on the 5来 calculation.
There are many ways to store such elements, and the elements themselves have their own rules and locations. (It is impossible to say that the two hashes are the same, the content is not the same, it is directly overwritten, not so idealized)
Don't worry, just use the algorithm to achieve it.
We are worried about this matter, if I want to query this array in the end there is no BA, first to the algorithm to calculate, get a position. Once this position is counted, go to this position and look for this element.
Like it? Compare AB First, found not the same, but this position more than one element, and then look down to BA, found that, this time will not be saved. This is the law within which it is produced, and does not conflict.
(In fact, I am more curious, hash conflicts, especially in the form of hanging, in memory is how the embodiment of the form?) )
After finishing, now look back and say, if we want to take care of the hash table, to store such objects, we should pay attention to the hashcode and Equals method to see the original example.
A string is stored in a hash table, and the Hashcode method is used when it is stored.
Let's look at what the Hashcode method of the string is. Find the String class, which itself overrides the Hashcode method in the object class, which defines the hash code in the string, or the hash algorithm, and also overrides the Equals method in object, and defines whether the content of its own judgment is the same.
Let's take a look at how it's done. Open the source string, outline on the left is the Outline window, which lists all the members of the current class, so it is more convenient to find.
Find Hashcode, which shows the string hash algorithm. How it counts, no matter what, it's based on the character of the string itself.
The value here is actually the array corresponding to the string.
Look at its equals, and it is also judging each one of them. The system itself has done the definition of hashcode and equals.
So far so much, to say something important, to store a few custom objects inside.
Java-preliminary Understanding-Common Object API (collection framework-hash table 2)