"Java Collection series five" HashMap parsing

Source: Internet
Author: User

2017-07-31 19:36:00

I. INTRODUCTION 1, HashMap function and usage scene

HashMap uses the array + one-way linked list to realize the storage function of the Key-value type data. The size of the HashMap is always the value of 2^x, primarily for more balanced use of the array position.

2. Data structure of storage key-value type

The following code, HASHMAP defines the node class, implements the Map.entry interface, entry interface only set and get method definition, extremely simple. node defines key, value, hash, and pointer to the next node Next,node class can actually be used as a one-way list.

3, the HashMap storage mode

HashMap uses an array + linked list to store the data, and the linked list is the node class mentioned earlier, with the following array:

4, hashmap several important parameters

To understand HashMap's scaling strategy, look at these default values:

The default capacity is 16, the maximum capacity is 2^30, and the default load factor is 0.75f. The next 3 treeify-related parameters are used when using the tree to store data for the time being.

Then look at several member variables:

Such as:

Table: An array of stored data;

EntrySet: Is the return value of EntrySet (), which is used as the cache, so that calling the EntrySet () method does not recreate a set object each time;

The number of size:key-value data;

Modcount: To prevent concurrent modification of the flag bit;

Threshold: critical value, exceeding this threshold need to enlarge;

Loadfactor: Load factor for calculating the critical value;

5. HashMap Initial capacity

The processing capacity of the HashMap is either the default value, or the default value is used by the user-set capacity. Using the new HashMap () constructor, the default capacity is used and the code is as follows:

Strange, why isn't there a new array here? The array is not created in the constructor, which indicates that the array was created using the lazy strategy, which must be created before the put element, or where is the data stored? Looking at the put method, the put method calls the Putval () of the package-level access permission, and Putval () calls the resize () with the following code:

See, if the table array is null, an array is created here.

So how did the user set the capacity hashmap initialized? Use the new HashMap (int capacity) constructor:

Finally, the constructor is called, and the load factor is the default value. There is no new array, but a critical value is computed. In the previous picture Resize () method, if the table has a size of 0 and Newthr is greater than 0, the array is created according to the size of newthr. The size of the HashMap mentioned above is always the power of 2, so now look at how NEWTHR is calculated? Look at the Tablesizefor () method:

The effect of this method is to find the smallest 2^n value greater than the CAP, so even if we set not a 2^n value, HashMap will help us. As for why it must be 2^n, explained in detail later.

=============================================================================================================== =====

Note: The above analysis is based on the JDK HashMap source code, from now on the use of Android HashMap source code analysis, the main reason is that the Android HashMap source code to remove some of the useless codes, the structure is clearer, easy to understand the principle.

=============================================================================================================== =====

Second, some important methods of HashMap 1, put (K key, V value) and the principle of expansion

As in the code, if the data key=null is joined, the following logic is executed:

"Java Collection series five" HashMap parsing

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.