1. 程式人生 > 其它 >Java Map原始碼學習筆記

Java Map原始碼學習筆記

集合框架原始碼解析

Map

HashMap

getNode

從hashMap中取出節點,首先判斷表是否為空表或者對應的節點為空,不是的話再判斷key值是否相同,相同的直接返回,不同的迴圈查詢,直至所有節點尋找完畢。

//hash – hash for key
//key – the key
final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
    	//判斷表是否為空,對應hash值的元素是否為空,為空直接返回null
        if ((tab = table) != null && (n = tab.length) > 0 &&(first = tab[(n - 1) & hash]) != null) {
            //如果key值相等,則返回該節點
            if (first.hash == hash &&((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            //判斷是否有next節點
            if ((e = first.next) != null) {
                //如果節點是TreeNode的例項,返回該tree中的對應key的節點
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                //否則迴圈直到找到對應的key值
                do {
                    if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }

putVal

向HashMap中放入值,首先判斷hash表是否為空表,如果是空表則先初始化hash表,隨後對key求hash值,判斷其在hash表中是否有空位,如有空位則放入該空位,如果該位置已經有元素了,則建立一個tree,將新節點放入其中,再根據引數中的onlyIfAbsent和evict來決定是否改變其節點的值。

/*
    hash – hash for key
    key – the key
    value – the value to put
    onlyIfAbsent – if true, don't change existing value 如果為true,則不更改現有值
    evict – if false, the table is in creation mode.    如果為false,則表處於建立模式。 
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
    	//如果表為空表,則初始化該表
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
    	//如果該key的hash值在表中對應的元素為空,則直接放入表中
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
    	//如果表中該項已存在,則按如下函式執行
        else {
            Node<K,V> e; K k;
            //判斷hash值相同的兩個鍵值對的key是否相同,如果相同將p賦值給e
            if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            //如果key值不同,先判斷p節點是否是TreeNode的例項,即判斷此節點是否存在一個Tree
            else if (p instanceof TreeNode)
                //如果存在,則將新的鍵值對放入樹中
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            //如果不存在,則建立一個TreeNode
            else {
                for (int binCount = 0; ; ++binCount) {
                    //p的next節點為空
                    if ((e = p.next) == null) {
                        //p的next指向新的節點
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    //p的next節點不為空,且key值與新鍵值對的key相同,結束迴圈
                    if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                //如果onlyIfAbsent == true 改變相同key的value值,返回oldvalue
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

resize