集合框架源码解析

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