Turn: HashMap Implementation Principle Analysis (Interview question: Two hashcode the same object how to deposit HashMap)

Source: Internet
Author: User

Original address: https://www.cnblogs.com/faunjoe88/p/7992319.html

Main content:

1) put question: If two key through Hash%entry[].length to get the same index, will there be a risk of coverage?

Here HashMap uses a concept of chained data structure. We mentioned above that there is a next property in the entry class that refers to the downward one entry.

For example, the first key value to a comes in, by calculating the hash of its key to get the index=0, remember to do: entry[0] = A. After a while. A key value pair B, by calculating its index is also equal to 0, now what?

HashMap will do this:B.next = A, entry[0] = B, if it comes in again C,index is equal to 0, then c.next = B, entry[0] = C;

In this way, we found that Index=0 's place actually accesses the A,b,c three key-value pairs, which are linked by the next property.

So don't worry about it. This means that the last inserted element is stored in the array. so far, the general realization of HASHMAP, we should have been clear.

Of course HashMap also contains some optimization aspects of the implementation, here also say. For example: the length of the entry[] is certain, with the data inside the map getting longer,

So the chain of the same index will be very long, will it affect performance? HashMap inside a factor, as the map size becomes larger, entry[] will be extended with a certain length of rules.

Turn: HashMap Implementation Principle Analysis (Interview question: Two hashcode the same object how to deposit 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.