Effectivejava (7) 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. Failure to do so would violate Object.hashcode's general conventions, resulting in the class not working properly with all hash-based collections including HashMap,

HashSet, Hashtable.

Object specification:

During the execution of an application, the Hashcode method must return the same integer as long as the information of the object's Equals method is not modified so that the same object is called multiple times. During multiple executions of the same application, each execution of the returned integer can be inconsistent.

If two objects are equal according to the Equals method, then the Hashcode method that calls either object in the two objects must produce the same result

If two objects are not equal according to the Equals method, then calling the Hashcode method of any of these two objects does not necessarily produce a different integer result. However, it is possible to improve the performance of the hash list by producing distinct integer results for unequal objects.

Effectivejava (7) Overwrite equals when always overwrite Hashcode

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.