First of all, to understand the role of hashCode, you must first know the collection in Java.
Generally speaking, there are two types of collections in Java, one is List, and the other is Set.
Do you know their difference? The elements in the former set are ordered, and the elements can be repeated; the elements in the latter set are unordered, but the elements cannot be repeated.
So here is a more serious problem: If you want to ensure that elements are not repeated, what should be the basis for judging whether two elements are repeated?
This is the Object.equals method. However, if you check once every time an element is added, then when there are many elements, the number of comparisons between elements added to the collection will be very large.
In other words, if there are now 1000 elements in the collection, then when the 1001st element is added to the collection, it will call the equals method 1000 times. This will obviously greatly reduce efficiency.
Therefore, Java adopts the principle of hash table. Hash is actually the name of a person. Because he proposed the concept of a hash algorithm, it was named after him.
Hash algorithm, also known as hash algorithm, directly assigns data to an address according to a specific algorithm. If you explain the hash algorithm in detail, it will require more article length, so I will not introduce it here.
Beginners can understand that the hashCode method actually returns the physical address of the object storage (actually it may not be the case).
In this way, when a new element is added to the collection, the hashCode method of this element is first called, and the physical location where it should be placed can be immediately located.
If there is no element at this position, it can be stored directly at this position without any comparison; if there is already an element at this position,
Just call its equals method to compare it with the new element. If it is the same, it will not be saved. If it is not the same, other addresses will be hashed.
So there is a problem of conflict resolution here. In this way, the number of actual calls to the equals method is greatly reduced, almost only once or twice.
Therefore, Java provides this for the eqauls method and hashCode method:
1. If two objects are the same, then their hashCode values must be the same; 2. If the hashCode of two objects are the same, they are not necessarily the same. The above mentioned objects being the same refers to comparison using the eqauls method.
Of course you don't have to do it as required, but you will find that the same object can appear in the Set collection. At the same time, the efficiency of adding new elements will be greatly reduced. The hashcode method is used to determine whether two objects are equal.
Then you would say, isn’t there a method called equals? Yes, these two methods are used to determine whether two objects are equal. But they are different. Generally speaking, the equals method is called by the user. If you want to determine whether two objects are equal,
You can override the equals method and call it in your code to determine whether they are equal. Simply put, the equals method is mainly used to determine whether two objects are equal from the surface or content. For example, there is a student class,
The only attributes are name and gender, so we can think that as long as the name and gender are equal, then the two objects are equal. The hashcode method is generally not called by users. For example, in hashmap, since the key cannot be repeated,
When he judged whether the key was repeated, he judged the hashcode method, and also used the equals method. What cannot be repeated here means that as long as one of equals and hashcode is not equal, it will be fine! So to put it simply, hashcode is equivalent to the encoding of an object, just like md5 in a file. It is different from equals in that it returns an int type, which is not intuitive in comparison. We generally overwrite hashcode when overriding equals to make their logic consistent. For example,
Still using the example just now, if the name and gender are equal, even if the two objects are equal, then the hashcode method should also return the hashcode value of the name plus the hashcode value of the gender, so logically, they are consistent.
To physically determine whether two objects are equal, just use ==