The theory of Put and get methods for HashMap in Java

Source: Internet
Author: User

Directly on the code

Note: The code comes from Java 9

Put method
public V put(K key, V value) {    return putVal(hash(key), key, value, false, true);}
    • When a put () is called, a hash value is generated first based on key, as follows:
static final int hash(Object key) {    int h;    //key 是 null 直接返回 0    //key 不是null,先计算key对应的hashCode,赋值给 h    //并将 h 与 h >>> 16 做异或(相同为0 不同为1)运算 ,作为hash值返回    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);}
    • For example, the process of bit operation, as explained in principle, refer to this article

    • After getting the hash value, call Putval () and do the following:
    • The object table is assigned a value to tab, and if the tab is empty as the first call to this method, it is resize () and assigns a value to tab,n;
    • Get the first element of tab: According to (N-1) & hash algorithm, calculate I find, if is empty, call NewNode (), Assign value to tab i;
    • If not empty, there may be 2 cases: the hash value is repeated, that is, the put process, found that the key corresponding to the value, the temporary e = p;
      As for the other situation is the position conflict, that is, according to (N-1) & hash algorithm has a collision, again sub-situation discussion;
      1. Deposit in the form of a linked list;
      2. If a collision causes the chain list to be too long (greater than or equal to Treeify_threshold), convert the linked list into a red-black tree;
    • Finally, if E is not empty, add e to the table (E.value is assigned the parameter value in Putval ());

The code is as follows:

Final V putval (int hash, K key, V value, Boolean onlyifabsent, Boolean evict) {node<k,v>[] tab; Node<k,v> p;    int n, I; The Tabel property in the HashMap object is empty---> First put---->resize () if (tab = table) = = NULL | |    (n = tab.length) = = 0) n = (tab = resize ()). length;    Found Tab[i] No value, direct deposit can (P = tab[i = (n-1) & hash] = = null) tab[i] = NewNode (hash, key, value, NULL); else {//tab[i] take the value, MO Panic, first define the below 2 variables node<k,v> e;        K K; If key is repeated very simply, direct E = P if (P.hash = = Hash && (k = p.key) = = Key | |            (Key! = null && key.equals (k))))        e = p; The chain is tree else if (p instanceof TreeNode) e = ((treenode<k,v>) p). puttreeval (This, tab, hash, key, VA        Lue);                    The chain is the linked list else {for (int bincount = 0;; ++bincount) {if ((E = p.next) = = null) {                    P.next = NewNode (hash, key, value, NULL); if (bIncount >= treeify_threshold-1)//-1 for 1st treeifybin (tab, hash);                Break } if (E.hash = = Hash && (k = e.key) = = Key | |                    (Key! = null && key.equals (k))))                Break            p = e;            }}///Several cases are processed, you can add elements if (E! = null) {//Existing mapping for key V oldValue = E.value;            if (!onlyifabsent | | oldValue = = NULL) E.value = value;            Afternodeaccess (e);        return oldValue;    }} ++modcount;    if (++size > Threshold) resize ();    Afternodeinsertion (evict); return null;}

How to put and get methods for HashMap in Java

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.