Java Collection source code analysis--hashmap

Source: Internet
Author: User
Tags array length

reprinted from Http://www.cnblogs.com/zhangyinhua/p/7698642.html#_label0 one, about the HashMap API definition
1. Hash table is based on the implementation of the map interface, this implementation provides map all operations, and provides the key and value can be null, (HashMap and Hashtable are roughly the same except that HashMap is asynchronous and allows key and value to be null) ,
This class does not determine the location of the elements in the map, specifically, this class is not sure whether the position of the element will remain unchanged over time.
2.Examples of HashMap have two parameters that affect performance, initialize capacity (initialcapacity), and loadfactor load factor.
Two, properties of HashMap
An instance of HashMap has two parameters that affect its performance.

Initial capacity: The number of buckets in the hash table load factor: A scale in which a hashtable can reach a full amount before its capacity increases automatically

When the number of entries in the hash table exceeds the current capacity * load factor (which is actually the actual capacity of HashMap), the Hashtable is rehash, expanding the hash table to twice times the number of buckets.

The default initial capacity in Java is 16, and the load factor is 0.75.        
HashMap hm=New HashMap (); for (int i=0;i<17;i++) { hm.put (i,i); } System.out.println (Hm.size ()); //17 System.out.println (hm.table.length); // +

1) Loadfactor loading factor

Definition: Loadfactor is translated into loading factor. The load factor is used to measure the degree of hashmap, which is the degree to which the data is stored in the array . The real-time loading factor for HashMap is calculated by size/capacity, rather than taking the number of buckets to divide by capacity.

    The more the Loadfactor is approaching 1, the more data (entry) is stored in the array, but when we get our value through key, we first find the position in the corresponding array by the hashcode value of the key.

If there are many elements in that location, then you need to compare the elements in the linked list with equals to get our value values so that the performance is very high,

    Loadfactor closer to 0, then the data stored in the array is more dilute, scattered too open, wasting a lot of space, so it is not good,

So the initial value of Loadfactor in HashMap is 0.75, and it is generally not necessary to change it.

2) Barrels

    Each position in the array is filled with a bucket, each of which is a linked list, and the list can have many elements (entry), which is the meaning of a bucket. It's the equivalent of putting elements in buckets.

3) capacity

Capacity is the capacity of the array represented by capacity , that is, the length of the array, and also the number of buckets in the HashMap. The default value is 16.

4) Meaning of size

Size is the number of elements actually stored in the instance of the HashMap

5) The role of threshold

Threshold = capacity * Loadfactor, when size>=threshold, it is necessary to consider the amplification of the array, that is to say, this means to measure whether the arrays need to be amplified by a standard .

Note that this is considered, because in fact, to amplify the array, in addition to this size>=threshold condition, another condition is required.

When will the size of the array be amplified? You can expand an array by size>=threshold it first when you put an element and also having elements at the corresponding array position.

Ii. source Code Analysis of HashMap 2.1, HashMap hierarchy relationship and inheritance structure

1) HASHMAP Inheritance structure

    

The above inherits a Abstractmap, which is used to alleviate the burden of writing the map interface.

2) Implement Interface

    

MAP<K,V>: The interfaces that have been implemented in the Abstractmap abstract class, which are implemented here, are actually superfluous. But every collection has this kind of error, and it doesn't have a big impact .

Cloneable: The ability to use the Clone () method, in HashMap, is to implement a shallow-level copy, that changes to the copied object will affect the copied object.

Serializable: The ability to serialize, that is, the HashMap object can be saved locally, and then the state can be restored.

2.2. Properties of the HashMap class
 Public classHashmap<k,v>extendsAbstractmap<k,v>ImplementsMap<k,v>, cloneable, Serializable {//Serial Number    Private Static Final LongSerialversionuid = 362498820763181265L; //The default initial capacity is    Static Final intdefault_initial_capacity = 1 << 4; //Maximum Capacity    Static Final intmaximum_capacity = 1 << 30; //The default fill factor    Static Final floatDefault_load_factor = 0.75f; //when the number of nodes on a bucket is greater than this value, it turns into a red-black tree.    Static Final intTreeify_threshold = 8; //Tree-to-chain list when the number of nodes on a bucket is less than this value    Static Final intUntreeify_threshold = 6; //The minimum size of a table in a bucket that is converted to a red-black tree    Static Final intMin_treeify_capacity = 64; //an array of stored elements, always a power of 2 times    transientNode<k,v>[] table; //set of specific elements to store    transientSet<map.entry<k,v>>EntrySet; //store the number of elements, note that this is not equal to the length of the array.     transient intsize; //each time you expand and change the counter of the map structure    transient intModcount; //Capacity expansion when critical worth actual size (volume * fill factor) exceeds critical value    intthreshold; //Fill Factor    Final floatLoadfactor;}
2.3, the construction method of HashMap

There are four construction methods, the function of the construction method is to record 16 this number to threshold (this value will eventually be treated as the first array length.) ) and initialize the load factor. Note that the table array in HashMap is already an array with no length at the beginning.

In the constructor, the size of the array is not initialized, the array is created at the beginning, the constructor does only two things, one is the initialization loading factor and the other is the size of the array initialization with threshold. Note is a record.

  

Third, HashMap source Analysis (ii) 3.1, put method
  /*
* 1. The table subscript is determined by the hash value of key
* 2. Find the table subscript and update the corresponding value if key exists
* 3. Call the AddEntry () method if key does not exist
*/
Public V put (K key, V value) {
if (table = = empty_table) {
Inflatetable (threshold);
}

if (key = = null)
return Putfornullkey (value);

int hash = hash (key);
int i = indexfor (hash, table.length);
for (entry<k,v> e = table[i]; E! = null; e = e.next) {
Object K;
The hash code is the same and the object is equal
if (E.hash = = Hash && (k = e.key) = = Key | | key.equals (k)) {
The new value replaces the old value and returns the old value
V oldValue = E.value;
E.value = value;
E.recordaccess (this);
return oldValue;
}
}
Add a new element when key does not exist
modcount++;
AddEntry (hash, key, value, I);
return null;
}

3.2. Get method
Final entry<k,v> getentry (Object key) {
if (size = = 0) {
return null;
}

int hash = (key = = null)? 0:hash (key);
for (entry<k,v> e = table[indexfor (hash, table.length)];
E! = null;
E = e.next) {
Object K;
if (E.hash = = Hash &&
(k = e.key) = = Key | | (Key! = null && key.equals (k))))
return e;
}
return null;
}
3.3. Resize method
 /*** Extended to the specified size*/    voidResizeintnewcapacity) {entry[] oldtable=table; intOldcapacity =oldtable.length; if(Oldcapacity = =maximum_capacity) {Threshold=Integer.max_value; return; } entry[] NewTable=NewEntry[newcapacity];        Transfer (newtable, inithashseedasneeded (newcapacity)); Table=newtable; Threshold= (int) math.min (newcapacity * loadfactor, maximum_capacity + 1); }
Iv. Summary 4.1, about array expansion

From the Putval source code we can know that when inserting an element size is added 1, if size is greater than threshold, it will be expanded. Assuming that our capacity size is 32,loadfator 0.75, then the threshold is a = * 0.75,

At this point, Insert 25 elements, and insert the 25 elements are in the same bucket, the data structure in the bucket is a red black tree, then there are 31 barrels is empty, will also be expanded processing, in fact, at this time, there are 31 barrels is empty, it seems that there is no need to expand the processing,

However, we need to expand the processing because our capacity size may not be appropriate at this time. As we know before, the expansion processing will traverse all the elements, the time complexity is very high, we also know that after one expansion process, the elements will be more evenly distributed in the buckets,

will improve access efficiency. So, to try to avoid the expansion of processing, it means that the traversal of the elements of the harm is greater than the elements in the bucket evenly distributed in the benefits.

4.2. Summary

1) to know that hashmap before JDK1.8 is a chain table hash such a data structure, and after JDK1.8 is an array of linked list plus red black tree data structure.

2) through the source of learning, HashMap is a quick key to get to the value of a collection, because the internal use of hash lookup is worth the method.

Java Collection source code analysis--hashmap

Related Article

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.