Equals and hashcode in the HashMap

Source: Internet
Author: User

Java object objects have 9 methods, which equals() and hashCode() in the implementation of HASHMAP play a more important role, I studied the source of HashMap, I met them, this blog is mainly to record their love between the killing.

In order to illustrate their relationship, we need hashmap background knowledge.

How the HashMap is stored

HashMap is implemented as an array chain, and different objects find corresponding array subscripts based on their hash code (the return value of the Hashcode method) and then deposit the array. What happens when different objects have the same hash code? This is solved by the chain in the array chain, the same hash code of the object is placed on the same chain, the chain of the chain head to the array, and then form the array chain.

When the first object has been stored in HashMap, and the second object is ready to be stored in HashMap, the system will call Equals () to check the relationship between them if they find their hashcode (that is, the conflict) after finding the array subscript, and there are two ways to handle them:
1. If it is equal, the system will no longer deposit a second object;
2. If unequal, the system regards them as purely subscript collisions, placing them on the same chain;

If their hashcode are not the same, they are deposited directly into the second object.

Equals () matches but Hashcode () differs

Now suppose there are two objects, and their equals () match, but Hashcode () is different, let's take a good look at what happens when they're in HashMap.

Assuming that Stringa and STRINGB are two different objects, the content is "Hello,world", Equals () returns True, but the hashcode () return value is not the same. We take Stringa and STRINGB as key, respectively, corresponding to Valuea and Valueb.

After Stringa and Valuea have deposited hashmap, we try to deposit stringb and Valueb, because Hashcode, STRINGB and Valueb smoothly into HashMap.

We write a query: Hashmap.get ("Hello,world"), what happens at this time? Are we Valuea or Valueb? Only God knows.

or change it, we write a query: Hashmap.get (Stringa), what happens at this time? Are we Valuea or Valueb? Only God knows.

Another change, we write a query: Hashmap.get (STRINGB), what happens at this time? Are we Valuea or Valueb? Only God knows.

Believe that smart you have seen the problem. So we always say, 如果equals匹配,hashCode()一定要相同 otherwise, something magical happens.

View Original

Equals and hashcode in the HashMap

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.