composite key 与hashcode,equals

1. 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; during the execution of the Java application, the same object is repeatedly Calls to the hashCode method must consistently return the same integer, provided that the information used to compare the objects for equals has not been modified.

2. If the two objects are the same, it is applicable to the equals(java.lang.Object) method, then the hashCode of the two objects must be the same; the equals(Object) method refers to the equals in the Object class that has not been rewritten by subclasses method

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

     If the two objects are not equal according to the equals(java.lang.Object) method, then calling the hashCode method on either of the two objects does not necessarily produce a different integer result. However, programmers should be aware that generating different integer results for unequal objects can improve hash table performance.

3. If the equals method of the object is rewritten, then the hashCode of the object is also rewritten as much as possible, and the object used to generate the hashCode must be the same as that used in the equals method, otherwise it will violate the second point mentioned above;

4. The hashCode of two objects is the same, which does not necessarily mean that the two objects are the same, that is, it does not necessarily apply to the equals(java.lang.Object) method, it can only indicate that the two objects are in the hash storage structure, such as Hashtable, they are "stored in the same basket".

Reference: https://www.sogou.com/link?url=DOb0bgH2eKh1ibpaMGjuy1Vv78Wo0PVv4fZWZOQHECGueKW9M67CaVLpMY1k7wxTVhRrW5zu__s.

 

 

Guess you like

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