技.艺.道:HashMap源码分析

提要:

  1. put(K key, V value) 
  2. hash(Object key) 
  3. putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) 
  4. resize()
  5. get(Object key)
  6. getNode(int hash, Object key)
  7. remove()
  8. removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable)
  9. replace(K key, V oldValue, V newValue)

详解:

 /**


    1.put(K key, V value) 方法:

    作用:内部调用了putVal()方法,对指定key的节点进行增改
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     将指定值与该map中的指定键相关联。 如果该map先前包含该键的map,则将替换旧值。
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

    /**


    2.hash(Object key) 方法


    参数:Object key,key对象。
    作用:俗称“扰动函数”,让hashCode的高16位也参与路由运算(若不做此操作,则在路由计算时:(n-1) & hashCode,当n的二进制数小于16位时,那么h的高16位将与0求与,特征将被短路掉,无法参与路由运算。)
    对key进行转换,若key为null,则返回0;否则返回一个“特殊值”。特殊值为key的hashCode 异或 key的hashCode无符号右移16位的结果值。
     * Computes key.hashCode() and spreads (XORs) higher bits of hash
     * to lower.  Because the table uses power-of-two masking, sets of
     * hashes that vary only in bits above the current mask will
     * always collide. (Among known examples are sets of Float keys
     * holding consecutive whole numbers in small tables.)  So we
     * apply a transform that spreads the impact of higher bits
     * downward. There is a tradeoff between speed, utility, and
     * quality of bit-spreading. Because many common sets of hashes
     * are already reasonably distributed (so don't benefit from
     * spreading), and because we use trees to handle large sets of
     * collisions in bins, we just XOR some shifted bits in the
     * cheapest possible way to reduce systematic lossage, as well as
     * to incorporate impact of the highest bits that would otherwise
     * never be used in index calculations because of table bounds.
    译文:计算key.hashCode()并将哈希的较高位(XOR)扩展为较低。 因为该表使用2的幂次掩码,所以仅在当前掩码上方的位中变化的哈希集将始终发生冲突。 (众所周知的示例是在小表中包含连续整数的Float键集。)因此,我们应用了一种变换,将向下传播较高位的影响。 在速度,实用性和位扩展质量之间需要权衡。 由于许多常见的哈希集已经合理分布(因此无法从扩展中受益),并且由于我们使用树来处理容器中的大量冲突集,因此我们仅以最便宜的方式对一些移位后的位进行XOR,以减少系统损失, 以及合并最高位的影响,否则由于表范围的限制,这些位将永远不会在索引计算中使用。
     */
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

    /**


    3.putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) 方法


    作用:向hashMap中添加元素或修改值
     * Implements Map.put and related methods
     *
     * @param hash hash for key :key的扰动结果
     * @param key the key :要插入的key
     * @param value the value to put:要插入的value
     * @param onlyIfAbsent if true, don't change existing value:如果是true,表示不改变已存在的值。
     * @param evict if false, the table is in creation mode.
     * @return previous value, or null if none
     */
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
        //tab:表示hashMap的散列表
        //p:表示当前散列表的一个元素
        //n:表示散列表数组的长度
        //i:表示路由寻址结果
        
        Node<K,V>[] tab; 
        Node<K,V> p; 
        int n, i;
        
        //延迟初始化 逻辑。第一次调用putVal()时进行初始化hashMap中的最耗费内存的散列表
        if ((tab = table) == null || (n = tab.length) == 0)//对n进行赋值
            n = (tab = resize()).length;
        
        //路由算法:(n - 1) & hash
        if ((p = tab[i = (n - 1) & hash]) == null)//当该桶位为空
            tab[i] = newNode(hash, key, value, null);//则新建一个节点存入数据
        else {
            Node<K,V> e;//一个临时节点
            K k;//一个临时key
            //当该桶位有元素,且该元素与当前待插入的数据的key相同,则将其赋值给e。后续需要对值进行替换。
            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) {
                    //如果该元素为尾节点,将待插入的数据放入新建的node中,放入链表尾部。
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        //如果当前链表的长度大于等于 树化阀值 ,那么就需要进行树化,将当前链表转化为红黑树
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    //当找到了一个与待插入数据的key相同的节点,需要进行替换操作
                    if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            //当e不等于null,说明找到了一个与待插入元素的key完全相同的数据,进行替换即可
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        //表示散列表结构的修改次数。增删次数。
        ++modCount;
        //size:散列表元素数。如果该值大于扩容阈值,则进行扩容。
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

    /**


    4.resize()方法


    作用:初始化 和 扩容
    实现流程:
     1.计算出扩容后的 数组大小 和 扩容阈值
     2.进行扩容
     * Initializes or doubles table size.  If null, allocates in
     * accord with initial capacity target held in field threshold.
     * Otherwise, because we are using power-of-two expansion, the
     * elements from each bin must either stay at same index, or move
     * with a power of two offset in the new table.
     *译文:初始化或增加表大小。 如果为空,则根据字段阈值中保持的初始容量目标进行分配。 否则,因为我们使用的是2的幂,所以每个bin中的元素必须保持相同的索引,或者在新表中以2的幂偏移。
     * @return the table
     */
    final Node<K,V>[] resize() {
        //引用扩容前的哈希表
        Node<K,V>[] oldTab = table;
        //扩容前的数组长度
        int oldCap = (oldTab == null) ? 0 : oldTab.length;
        //扩容前的 扩容阈值,即触发本次扩容的阈值
        int oldThr = threshold;
        
        //扩容之后的 数组长度和扩容阈值
        int newCap, newThr = 0;
        //条件成立说明hashMap中的散列表已经初始化过了,是一次正常的扩容
        if (oldCap > 0) {
            //扩容之前的table数组大小已经达到最大阈值后,则不扩容,且设置扩容条件为int的最大值。(隐含:再也不会扩容)
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }
            //oldCap左移一位数值翻倍,并且赋值给newCap。newCap小于数组最大值限制 且扩容之前的阈值 >= 16
            //这种情况下,则下次扩容的阈值 等于当前阈值的两倍
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                newThr = oldThr << 1; // double threshold
        }
        //oldCap == 0.说明hashMap中的散列表是null
        //1.new hashMap(initCap, loadFactor)
        //2.new hashMap(initCap)
        //3.new hashMap(map);且这个map有数据
        else if (oldThr > 0) // initial capacity was placed in threshold
            newCap = oldThr;
        //oldCap == 0.oldThr == 0
        //new HashMap();
        
        else {               // zero initial threshold signifies using defaults
            newCap = DEFAULT_INITIAL_CAPACITY;//16
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);//负载因子 * 数组长度 = 12
        }
        //newThr为0时,通过newCap和LoadFactor计算出一个newThr 
        if (newThr == 0) {
            float ft = (float)newCap * loadFactor;
            newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                      (int)ft : Integer.MAX_VALUE);
        }
        //更新扩容阈值
        threshold = newThr;
        
        //--------真正开始进行扩容----------
        //创建一个更长更大的数组
        @SuppressWarnings({"rawtypes","unchecked"})
            Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
        table = newTab;
        //hashMap本次扩容之前table不为null。即oldTab已经指向了一个数组。(数组中有无数据尚未可知)
        if (oldTab != null) {
            //遍历老 table
            for (int j = 0; j < oldCap; ++j) {
                //定义一个临时节点
                Node<K,V> e;
                //说明当前桶位有数据。可能为 单个数据,链表,红黑树。
                if ((e = oldTab[j]) != null) {
                    //方便JVM GC回收内存
                    oldTab[j] = null;
                    //情况一:表明 e是一个单独的元素
                    if (e.next == null)
                        //通过 路由算法 得到 当前桶位元素在新数组中的位置,并放入。
                        newTab[e.hash & (newCap - 1)] = e;
                    //情况二:如果该元素是一个树节点
                    else if (e instanceof TreeNode)
                        //
                        ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                        
                    //情况三:如果该元素是一个链表节点
                    else { // preserve order
                        //桶位已经形成链表
                        //低位链表:存放在扩容之后的数组的下标位置,与当前数组的下标位置一致(原来链表的高位为0的元素放在这里)
                        Node<K,V> loHead = null, loTail = null;
                        //高位链表:存放在扩容之后的数组的下标位置,为当前数组下标位置加上扩容前的数组长度。(原来链表的高位为1的元素放在这里)
                        Node<K,V> hiHead = null, hiTail = null;
                        Node<K,V> next;
                        do {
                            next = e.next;
                            //假如是在13这个位置,
                            //hash-> ... 1 1101
                            //hash-> ... 0 1101
                            // 0b 1 0000 ------> 
                            //实现了 当第5位为 0 则放在低位链表中。
                            if ((e.hash & oldCap) == 0) {
                                if (loTail == null)
                                    loHead = e;
                                else
                                    loTail.next = e;
                                loTail = e;
                            }
                            else {
                                if (hiTail == null)
                                    hiHead = e;
                                else
                                    hiTail.next = e;
                                hiTail = e;
                            }
                        } while ((e = next) != null);
                        //当低位链表的中有数据,低位链表初始化尾节点(切断尾节点与其原next节点的联系)。头结点需要放入数组对应的桶位。
                        if (loTail != null) {
                            loTail.next = null;
                            newTab[j] = loHead;
                        }
                        //当高位链表的中有数据,高位链表初始化尾节点(切断尾节点与其原next节点的联系)。头结点需要放入数组对应的桶位。
                        if (hiTail != null) {
                            hiTail.next = null;
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
        return newTab;
    }

   /**


   5.get(Object key)方法


   作用:根据key获取hashMap中存储的数据
   
     * Returns the value to which the specified key is mapped,
     * or {@code null} if this map contains no mapping for the key.
     *译文:返回指定键所映射到的值;如果此映射不包含键的映射关系,则返回{@code null}。
     
     * <p>More formally, if this map contains a mapping from a key
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
     * key.equals(k))}, then this method returns {@code v}; otherwise
     * it returns {@code null}.  (There can be at most one such mapping.)
     *译文:<p>更正式地说,如果此映射包含从键{@code k}到值{@code v}的映射,使得{@code(key == null?k == null:key.equals(k) )},则此方法返回{@code v}; 否则返回{@code null}。 (最多可以有一个这样的映射。)
     
     * <p>A return value of {@code null} does not <i>necessarily</i>
     * indicate that the map contains no mapping for the key; it's also
     * possible that the map explicitly maps the key to {@code null}.
     * The {@link #containsKey containsKey} operation may be used to
     * distinguish these two cases.
     *译文:<p>返回值{@code null}并不<i>不必要</ i>表示映射不包含该键的映射; 映射也可能将键显式映射到{@code null}。 {@link #containsKey containsKey}操作可以用来区分这两种情况。
     * @see #put(Object, Object)
     */
    public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }
    
    /**


    6.getNode(int hash, Object key)方法


    作用:获取hashMap中的node中的数据
     * Implements Map.get and related methods
     *
     * @param hash hash for key
     * @param key the key
     * @return the node, or null if none
     */
    final Node<K,V> getNode(int hash, Object key) {
        //引用当前hashMap的散列表
        Node<K,V>[] tab; 
        //first:桶位中的头元素;
        //e:临时node元素
        Node<K,V> first, e; 
        
        int n; K k;
        //当 该hashMap中有数据。
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {//路由寻址 找到该位置的元素,赋值为头元素。当它不为空时。
            
            //第一种情况:当得到的头元素就是要找的元素,直接返回即可
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))//key != null && key.equals(k) 是为了比较的更完善一些,先判空是为了避免后面调用equals()时出现空指针异常。
                return first;
            //当该节点为链表节点或红黑树节点
            if ((e = first.next) != null) {
                //第二种情况:当该节点是树节点
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                //第三种情况:该节点为链表,遍历该链表,寻找要找的节点
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;//直到找到,
                } while ((e = e.next) != null);
            }
        }
        return null;//或找不到
    }
    
    
    /**


    7.remove()方法


    作用:删除指定key对应的节点
     * Removes the mapping for the specified key from this map if present.
     *译文:此映射中指定键的映射(如果存在)。
     
     * @param  key key whose mapping is to be removed from the map
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     
     
     */
    public V remove(Object key) {
        Node<K,V> e;
        return (e = removeNode(hash(key), key, null, false, true)) == null ?
            null : e.value;
    }
    
    /***
    remove(Object key)的同名方法
    作用:当key与value都满足条件才能删除
    */
    @Override
    public boolean remove(Object key, Object value) {
        return removeNode(hash(key), key, value, true, true) != null;
    }
    
    
    /**


    8.removeNode(int hash, Object key, Object value, boolean matchValue, boolean movable)方法


    作用:删除指定节点
    matchValue:当它为true时,value也需要匹配上。
    
     * Implements Map.remove and related methods
     *
     * @param hash hash for key
     * @param key the key
     * @param value the value to match if matchValue, else ignored
     * @param matchValue if true only remove if value is equal
     * @param movable if false do not move other nodes while removing
     * @return the node, or null if none
     */
    final Node<K,V> removeNode(int hash, Object key, Object value,
                               boolean matchValue, boolean movable) {
                                   
        //tab:引用当前hashMap的散列表
        Node<K,V>[] tab;
        //当前的node元素
        Node<K,V> p; 
        //n表示散列表数组的长度,index表示寻址结果
        int n, index;
        
        
        //(tab = table) != null   table指向了一个数组,而不是null
        //且 该数组的长度大于0
        //且 该元素所在的位置 有数据节点 而不是null
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (p = tab[index = (n - 1) & hash]) != null) {
                
            //node为查找到的结果,e表示当前node的下一个元素
            Node<K,V> node = null, e; 
            K k; V v;
            
            //情况一:当不用下钻就找到了 要找的 节点,即该节点为某个桶位的第一个元素。
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                node = p;
            
            //当该节点是链表或树(节点)
            else if ((e = p.next) != null) {
                //情况二:当该节点是树节点时
                if (p instanceof TreeNode)
                    node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
                //当该节点是链表时
                else {
                    //遍历 链表
                    do {
                        //情况三:找到要找的元素,赋值给node,跳出循环即可
                        if (e.hash == hash &&
                            ((k = e.key) == key ||
                             (key != null && key.equals(k)))) {
                            node = e;
                            break;
                        }
                        //更新p的指向,可以保留找到链表中目标节点前的那个节点。
                        p = e;
                    } while ((e = e.next) != null);
                }
            }
            
            //当node不为空,即已经按照key查找到数据了,根据matchValue的boolean值判断是否需要比较value值,当其为true时需要比较。
            if (node != null && (!matchValue || (v = node.value) == value ||
                                 (value != null && value.equals(v)))) {
                //进入删除操作
                //情况一:当 当前节点是 红黑树 节点时
                if (node instanceof TreeNode)
                    ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
                //情况二:当 数组中的某个桶位上的第一个元素即为要 删的元素
                else if (node == p)
                    //将当前节点 短路(删除) 掉
                    tab[index] = node.next;
                else
                    //情况三:当 节点为链表中的一个非头节点时,将node节点短路掉即可。
                    p.next = node.next;
                //结构修改次数加1
                ++modCount;
                //kv对的数量减1
                --size;
                afterNodeRemoval(node);
                return node;
            }
        }
        return null;
    }

    /***


    9.replace(K key, V oldValue, V newValue)方法


    作用:替换指定key和value的value
    */
    @Override
    public boolean replace(K key, V oldValue, V newValue) {
        Node<K,V> e; V v;
        if ((e = getNode(hash(key), key)) != null &&
            ((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
            e.value = newValue;
            afterNodeAccess(e);
            return true;
        }
        return false;
    }
    /***
    作用:替换指定key的value
    */
    @Override
    public V replace(K key, V value) {
        Node<K,V> e;
        if ((e = getNode(hash(key), key)) != null) {
            V oldValue = e.value;
            e.value = value;
            afterNodeAccess(e);
            return oldValue;
        }
        return null;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章