Java objects overwrite equals when always overwrite Hashcode

Source: Internet
Author: User

In each class that overrides the Equals method, you must also overwrite the Hashcode method.

General conventions for Hashcode:

1 during the execution of a program, the Hashcode method must consistently return the same integer as long as the information used to compare the Equals method of the object has not been modified, and the same object is called multiple times.

2 If two objects are equal according to the Equals (object) method, the Hashcode method that calls either object in both objects must produce the same integer result.

3 If two objects are not equal according to the Equals (object) method, then calling the Hashcode method of any of these two objects does not necessarily produce a different integer result. The performance of the hash table can be improved by producing different integer results for unequal objects.

The key convention that is violated because there is no overwrite hashcode is 2nd: An equal object must have an equal hash code (hash code). Because the class does not overwrite the Hashcode method, two equal instances call the Hashcode method to return an integer that may not be equal.

  

A good hash function generates unequal hash codes for unequal objects. Ideally, the hash function should distribute the unequal instances of the collection evenly across all possible hash values. 31 can get better performance by replacing multiplication with shift and subtraction: 31*i== (i<<5)-I.

Resources

"Effective Java Chinese Version 2nd edition" 9th: Overwrite equals when always overwrite hashcode p39-43

Java objects overwrite equals when always overwrite Hashcode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.