Java Collection-hashmap

Source: Internet
Author: User

HashMap

>>>: Move right

^: Bitwise XOR OR

&: Logic and


Final V putval (int hash, K key, V value, Boolean onlyifabsent,         & nbsp;         Boolean evict) {        node<k,v>[] tab; Node<k,v> p; int N, i;        if (tab = table) = = NULL | | (n = tab.length) = = 0)//First initialization tab            n = (tab = Resize () ) .length;        if (p = tab[i = (n-1) & hash]) = = = NULL)//determines the storage location based on the hash value of the key &nbsp ;           Tab[i] = NewNode (hash, key, value, NULL);         Else {            node<k ,v> e; K k;            if (P.hash = = Hash &&                 ((k = p.key) = = Key | | (Key! = null && key.equals (k))))                 e = p;             else if (P instanceof TreeNode)                  e = ((treenode<k,v>) p). Puttreeval (This, tab, hash, key, value);            else {                 for (int bincount = 0;; ++bincount) { & nbsp;                  if ( (e = p.next) = = null) {                         P.next = NewNode (hash, key, value, NULL);                         if ( Bincount >= treeify_threshold-1)//-1 for 1st           & nbsp;                treeifyBin (tab, Hash);                         break;                    }                     if (E.hash = = Hash &&                         ( K = e.key) = = Key | | (Key! = null && key.equals (k))))                         break;                     p = e;                }            }             if (E! = null) {//existing mapping for key           & nbsp;    V oldValue = e.value;                 if (!onlyifabsent | | oldValue = = NULL)                      E.value = value;                 afternodeaccess (e);                return oldvalue;            }       }         ++modcount;        if (++size > Threshold)              Resize ();        Afternodeinsertion (evict);        return null;   }
each time you put an element into HashMap, a node object is new, and the position is determined by calculating the hashcode value of the key. If two elements have the same hashcode, they will be placed on the same index. The problem arises, how do you put it? Originally it was stored in the form of a list (LinkedList) (logically).



The hash of key is worthy of calculation method

Static final int hash (Object key) {        int h;        return (key = = null)? 0: (H = key.hashcode ()) ^ (h >>> 16); Move right first, then bitwise XOR.    }

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