The realization principle of HashMap

Source: Internet
Author: User

Hash algorithm (HashMap Implementation principle )

Java The implemented hash table

1.HashMap the data structure

  The characteristics of the array are: Easy addressing, insertion and deletion difficulties, and the list is characterized by: difficult to address, insert and delete easy. Can we combine the characteristics of both to make a data structure that is easy to address, insert and delete? The answer is yes, this is the hash table We are going to mention, the hash table has a number of different implementations, and I will explain the most commonly used method - The zipper method, which we can understand as " array of linked lists " ,

from the We can find that the hash table is made up of arrays+A linked list, a length of -array, each element stores the head node of a linked list. So what rules are these elements stored in the array? The general situation is throughHash (key)%lenobtained, i.e. the element'sKeythe hash value of the array is obtained by modulo the length of the array. For example, in the above hash table,12%16=12,28%16=12,108%16=12,140%16=12. So A, -,108as well $are stored in the array subscript as Athe location.

  HashMap is actually a linear array implementation. , so it can be understood that the container in which the data is stored is a linear array. This may be confusing to us, how does a linear array implement key-value pairs to access data? Here HashMap has to do some processing.

  1.FirstHashMapimplement a static inner class insideEntry, its important attributes arekey, value, next, from the propertyKey,valuewe can clearly see it.Entryis thatHashMapa basis for implementation of key-value pairsBean, we said aboveHashMapis based on a linear array, the array isentry[],Mapthe contents are stored inentry[]inside.

2.HashMap the Access Implementation

Since it is a linear array, why random access? Here HashMap uses a small algorithm, which is generally implemented as follows:

When storing:

int hash = Key.hashcode (); //This hashcode method is not detailed here, as long as understanding the hash of each key is a fixed int value

int index = hash% Entry[].length;

Entry[index] = value;

When values are taken:

int hash = Key.hashcode ();

int index = hash% Entry[].length;

return Entry[index];

It 's easy to understand when we get here. HashMap basic principle of access through key-value pairs

3. question: If two keys get the same index by hash%entry[].length , will there be a danger of coverage?

hereHashMapIt uses a concept of chained data structures. We mentioned it above.Entryclass has aNextproperty, which refers to the downward oneEntry. To make an analogy,first key-value pairAcome in, by calculating itsKeyof theHashGet theindex=0, remember to do: entry[0] = A. After a while, a key-value pair came in .B, by calculating itsIndexalso equals0, what now? HashMapwould do that: B.next = a,entry[0] = B,if you come in againC,indexalso equals0,soC.next = b,entry[0] = C; so that we findindex=0the place actually access theA,b,cthree key-value pairs,them throughNextThis property is linked together. So don't worry about it. This means that the last inserted element is stored in the array. until this point,HashMapThe general realization that we should have been clear.

of course hashmap entry[] After a certain length, with map The data is getting longer, so the same index hashmap Span style= "font-family:arial" >map size entry[]

3.< /span> resolve hash conflict method

    1. Open addressing Method (linear detection re-hash, two-time detection and re-hash, pseudo-random detection and hashing)
    2. Re-hash method
    3. Chain Address method
    4. Create a public overflow zone

      Java in HashMap the solution is to use the chain address method.

The realization principle of 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.