HashMap principle Detailed

Source: Internet
Author: User

HashMap

a definition and creation

HASHMAP implements the map interface and inherits the Abstractmap class. The basic functionality of map is included in the Abstractmap.

(1) Initial size

Static int 1 4 // aka

From the source can be seen size is 16 (1 left to move 4 bit 1000 = 16)

Static Final int maximum_capacity = 1 << 30;

The maximum length is 2 of the 30 square 1073741824 is basically able to meet the majority of the needs of the use.

Static Final float Default_load_factor = 0.75f;

The default load factor is 0.75, and the default load factor generally does not need to be changed. If you pay more attention to the memory space, but not the speed, you can adjust the load factor, conversely, if you pay more attention to HashMap read and write speed, but not too much attention to memory space, you can adjust the small load factor.

two put method, HashMap write value, get method Read Value

The HashMap from 1.7 was implemented with an array of table+ linked lists, while the 1.8JDK Squadron HashMap was optimized by arrays, linked lists, red-black trees (a binary tree) to achieve, linked list of the array length of more than 8 o'clock, turned into a red black tree to improve the efficiency of the hashmap.

(1) 1.7 HashMap put method in detail:

 Publicv put (K key, V value) {//If key is null, call the Putfornullkey method to process if(Key = =NULL)        returnPutfornullkey (value); //Hash value is calculated based on the keycode of key, and the hash function continues to calculate the hash value for a mathematical method using bit operation. inthash =Hash (Key.hashcode ()); //find the index of the hash value in the table (here the Indexfor Way is a mathematical method, the return value is hash&table.length-1, through the bitwise operation,ensure that the return value of this function is always less than or equal to table.length, which ensures that the I value is within the index of the table array)     inti =indexfor (hash, table.length); //if the Entry at the I index is not NULL, the next element of the E element is traversed continuously through the loop//The next value in the entry stored in table I records the linked list situation, there may be multiple entry chains, so the list is recycled  for(entry<k,v> e = table[i]; E! =NULL; E =e.next) {Object k; //1. Locate the key with the same hash value as the insertion value, and the value of key is the same as the key value inserted.//2. Both meet to determine the insertion of the same key (the hash value of different key values may be equal (hash collision), so here to increase the value of the key itself to judge)     if(E.hash = = Hash && (k = e.key) = =Key||Key.equals (k))) {            //The key value is replaced directly by the value, and the function jumps outV OldValue =E.value; E.value=value; E.recordaccess ( This); returnOldValue; }    }    //if Entry at index i is null or key has the same hash value and key is different, you need to add Entrymodcount++; //add key, value to index at Iaddentry (hash, key, value, I); return NULL; }   

Below is a brief description of the process through the Viso diagram

AddEntry This function here in a simple word, first on the code
//new Entry in table array nodevoidAddEntry (intHash, K key, V value,intBucketindex) {       //gets the Entry at the specified Bucketindex indexEntry<k,v> e = Table[bucketindex];//①//Place the newly created Entry into the Bucketindex index and let the new Entry point to the original Entry//There are two types of situations here//1.table no element in I, then the value of E is null, the newly inserted entry's next is null, no linked list//The element is present in the 2.table, and the newly inserted element overwrites the position in the array, while the newly created entry in the next point points to the old element, forming the linked list. Table[bucketindex] =NewEntry<k,v>(hash, key, value, E); //if the number of key-value pairs in the Map exceeds the limit    if(size++ >=threshold)//extends the length of the table object to twice times. Resize (2 * table.length);//②}

Add the entry element at the specified position in table, if there is already a entry element, replace the old element with the new entry, but in entry the next record the position of the original element, implement the linked list query.

Here basically the Put method principle is basically clear, the following look at the Get method

 PublicV get (Object key) {//If key is null, call Getfornullkey to remove the corresponding value if(Key = =NULL)        returnGetfornullkey (); //calculates its hash code based on the hashcode value of the key inthash =Hash (Key.hashcode ()); //directly takes the value at the specified index in the table array,  for(Entry<k,v> e =table[indexfor (hash, table.length)]; E!=NULL; //search for the next Entr of the Entry chainE = e.next)//①{Object k; //If the Entry key is the same as the key being searched     if(E.hash = = Hash && (k = e.key) = =Key||Key.equals (k))) returnE.value; }    return NULL; } 

First through the hash method in the array position, find out entry and through the entry next do the traversal, find the value of the key values corresponding

HashMap principle Detailed

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.