hashcode

hashcode 

hashCode is the value of int type calculated by jdk according to the address or string or number of the object. For details, please refer to [1] public int hashCode() returns the hash code value of the object. This method is supported to improve the performance of hash tables such as those provided by java.util.Hashtable.

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result

If the results returned by two hashCode() are equal, the equals methods of the two objects are not necessarily equal.

 

The existence of HashCode is mainly used for the quickness of search, such as Hashtable, HashMap, etc. HashCode is used to determine the storage address of the object in the hash storage structure;

 

 

How to understand the role of HashCode:

From the perspective of Object, every time JVM creates an Object, it will throw the Object into a Hash table. In this case, the next time the Object is compared or the object is retrieved (reading process), it will be based on the object's HashCode then fetches this object from the Hash table. The purpose of this is to improve the efficiency of fetching objects. If the HashCode is the same, then call equal.

 

 

It is obviously too inefficient to use equal() for all the comparisons that require a lot of speed and speed, so the solution is to use hashCode() to compare first whenever you need to compare. If the hashCode() is different, then Indicates that the two objects are definitely not equal (that is, there is no need to use equal() to compare again). If the hashCode() is the same, then compare their equal() at this time. If equal() is also the same, it means that the two The objects are really the same, which can greatly improve the efficiency and ensure the absolute correctness of the comparison!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326271640&siteId=291194637