Java Foundation-hashmap

Source: Internet
Author: User

Jdk7 HashMap Implementation of the idea is relatively simple, is a entry array, each entry in the array is a list of the starting point (table header).

1      Publicv put (K key, V value) {2         if(Table = =empty_table) {3 inflatetable (threshold);4         }5         //If key is null, the entry is placed in the No. 0 bit6         if(Key = =NULL)7             returnPutfornullkey (value);8         inthash =hash (key);9         inti =indexfor (hash, table.length);Ten         //checks if the key exists in the list of I-bit lists and, if present, replaces the original value with the new value One          for(entry<k,v> e = table[i]; E! =NULL; E =e.next) { A Object K; -             if(E.hash = = Hash && (k = e.key) = = Key | |Key.equals (k))) { -V OldValue =E.value; theE.value =value; -E.recordaccess ( This); -                 //and returns the old value -                 returnOldValue; +             } -         } +  Amodcount++; at         //if not present, add entry to the I-bit list header if (size >= threshold) && (null! = table[Bucketindex]) True (threshold=capacity*loadfactor),     also involves resize (expansion, resize (2 * table) .  length);  ) Operation - addentry (hash, key, value, I); -         return NULL; -}

The hash function of JDK7

1     Final intHash (Object k) {2         inth =Hashseed;3         if(0! = h && kinstanceofString) {4             returnSun.misc.Hashing.stringHash32 (String) k);5         }6 7H ^=K.hashcode ();8 9         //This function ensures, hashcodes that differTen         //constant multiples at each bit position has a bounded One         //Number of collisions (approximately 8 at default load factor). AH ^= (H >>>) ^ (H >>> 12); -         returnH ^ (H >>> 7) ^ (H >>> 4); -}

There is a problem with HashMap in Jdk7, if the hash value of key is mapped to the same bucket, the lookup of HashMap will degenerate into sequential lookup, which will greatly affect the lookup performance (no effect on insert performance). Jdk8 in order to solve this problem, the HashMap has made some improvements, when a bucket list length is greater than the built-in threshold (8), the bucket list will be tree (treeify), the linked list into a red black tree (the rest of the list will not be affected).

1      Public v put (K key, V value) {2         return false true ); 3     }

The hash function of JDK8

1     Static Final int Hash (Object key) {2         int h; 3         // Hashcode returns an int total of 32 bits, which is the result that the high 16 bits remain the same, low 16 bits with a height of 16 or a new low 16 bit after the formation. This is done in order to prevent the hashcode low 0, after the mapping (hash& (TABLE.LENGTH-1)) will be clustered in No. 0 place.  4         returnnull)? 0: (H = key.hashcode ()) ^ (H >>> +); 5     }

1     FinalV Putval (intHash, K key, V value,BooleanOnlyifabsent,2                    Booleanevict) {3         //tab is table,p is the precursor node, n is the table length, I is the index of key4node<k,v>[] tab; Node<k,v> p;intn, I;5         if(tab = table) = =NULL|| (n = tab.length) = = 0)6n = (tab =resize ()). length;7         //If the first bit does not already have a node, insert it directly8         if(p = tab[i = (n-1) & hash]) = =NULL)9Tab[i] = NewNode (hash, key, value,NULL);Ten         Else{//situation where a node already exists in the first bit OneNode<k,v>e; K K; A             //the key for the first node is the same as the key that will be inserted -             if(P.hash = = Hash && -(k = p.key) = = Key | | (Key! =NULL&&key.equals (k) ))) theE =p; -             //If the first node is TreeNode, it indicates that the bit is a red-black tree, inserted directly -             Else if(pinstanceofTreeNode) -E = ((treenode<k,v>) p). Puttreeval ( This, tab, hash, key, value); +             //traversing a linked list for substitution or insertion -             Else { +                  for(intBincount = 0;; ++Bincount) { A                 //if P.next is null, it is inserted at the end of the list at                 if((e = p.next) = =NULL) { -P.next = NewNode (hash, key, value,NULL); -                         //If the list length is greater than treeify_threshold (8, not set) then the tree -                         if(Bincount >= treeify_threshold-1)//-1 for 1st -Treeifybin (tab, hash);//The list of tree-bits is a red-black tree -                          Break; in                     } -                     //If the same key is found, it jumps directly, and subsequent updates the value to                     if(E.hash = = Hash && +(k = e.key) = = Key | | (Key! =NULL&&key.equals (k) ))) -                          Break; thep =e; *                 } $             }Panax Notoginseng             //Update Value -             if(E! =NULL) {//existing mapping for key theV OldValue =E.value; +                 if(!onlyifabsent | | oldValue = =NULL) AE.value =value; the afternodeaccess (e); +                 returnOldValue; -             } $         } $++Modcount; -         if(++size >threshold) - resize (); the afternodeinsertion (evict); -         return NULL;Wuyi}

Java Foundation-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.