Why do hashcode must be overwritten when equals is overwritten?

Source: Internet
Author: User

Why do we say "hashcode must be overwritten when equals is overwritten" in many places ". Speaking of this, I know many people know that there is a principle:If a. Equals (B), make sure a. hashcode () = B. hashcode (). Why? Hashcode and what I wroteProgramThe business logic is irrelevant. Why do I need to override? Want me to sayIf your class is never placed in a hash code-based container, you do not have to override hashcode ()

Put it exactly in hashmap and hashtable. If it is used as value rather than as key, it does not need to override hashcode. As for hashset, it only ignores the hashmap of value. Every time hashset. Add (o) is actually hashmap. Put (O, dummyobject ).

So why overide hashcode is needed in the hash container? Because the hashmap for each get operation depends on whether equals is true or whether hash code is consistent, and the put operation also depends on equals and hash code.

If you still don't understand it here, let's take an example:

For example, put a custom class Foo {...} as the key into hashmap. In fact, hashmap also stores data in an array, so in the put function, hashmap calls Foo. hashcode () calculates the subscript of this element in the array, and encapsulates the key and value into an object into an array. Wait, what if the hash code calculated by the two objects is the same? Will it be washed out? Answer the first 2nd questions. If it is false, it depends on foo. Equals (). If equals () is true, it will be washed out. In case of false, it is the so-called collision. When the two elements have the same hashcode but equals is false, the element of the array in the hashmap becomes a linked list. That is, the elements with the same hash code are in a linked list, and the head of the linked list is in that array.

Back to get, hashmap first calls key. hashcode () to calculate the array subscript, and then checks that equals is found if it is true, so equals is involved.

Assume that an element whose key is a is in hashmap:

1: If equals is set to true but B with different hashcode is used as the get parameter, the array subscript calculated by B is definitely not the subscript position of.

2: If equals is set to false but B with the same hashcode is used as the get parameter at this time, the array subscript calculated by B is correct, however, if equals is used to find a matched key, a cannot be found.

In the above two cases, either get cannot find the matching element and return NULL, or return a hashcode and equals that exactly match B's other elements, which leads to confusion. The root of chaos is the implementation of hashcode and equals by mistake.

MARK: For more information, see Item 9 of Bloch's : always override hashcode when you override equals

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.