HashMap源码学习——存储原理putVal

在 Java8 之前,其底层实现是数组 + 链表实现,Java8 使用了数组 + 链表 + 红黑树实现。本文看的是Java8代码

1.了解存储方式主要了解put方法(putVal)


    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) {
        //HashMap的Node数组
        Node<K,V>[] tab;
        //要操作的Node
        Node<K,V> p;
        //n为当前HashMap的Node数组长度  i为操作的数组下标
        int n, i;
        //HashMap为空时
        if ((tab = table) == null || (n = tab.length) == 0){
            //resize()方法
            n = (tab = resize()).length;
        }
        //用key的hash值和(n-1)计算出当前插入的数组下标  当此处为空时只要new一个Node就可以了
        //当此处不为空时 进行后续操作
        if ((p = tab[i = (n - 1) & hash]) == null){
            tab[i] = newNode(hash, key, value, null);
        }else {
            Node<K,V> e; K k;
            //插入key的刚好是数组Node的key
            if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k)))){
                e = p;
                //当插入key不是数组Node的key的时候  先判断是链表结构还是红黑树
                //如果是红黑树,添加红黑树节点
            } else if (p instanceof TreeNode) {
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
                //当前不是红黑树
            }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;
                }
            }
            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;
    }

2.putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict)参数解读

hash:根据key生成hash值。

static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

key、value就不用说了吧!

onlyIfAbsent:当要插入的key已存在时是否替换value,false则替换,true则不替换。

     if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }

evict:在HashMap中无作用,afterNodeInsertion方法在LinkedHashMap实现。

if (++size > threshold)
    resize();
afterNodeInsertion(evict);
return null;

3.put过程总结 

  1. 根据key生成hash值。
  2. 根据key的hash和Node数组长度生成下标,找到对应数组元素。
  3. 对应元素为空时直接new一个塞进去。2
  4. 对应元素不为空且key正好和插入的key相同时,替换元素的值。1
  5. 对应元素不为空且key和要插入的key不同时判断对应元素是不是TreeNode,是的话插入元素。6或7
  6. 不是的话说明后面的是NULL或链表,找对应元素的nextNode。
  7. nextNode为NULL时new一个Node塞进去。4
  8. nextNode不为NULL但是key和要插入的key相同时,修改nextNode的值。5
  9. nextNode不为NULL且key和要插入的key不同时就去找nextNode的nextNode知道找到NULL或key相同的nextNode。4或5
  10. 在n个nextNode之后找到NULL后,要判断长度是否超过默认的链表允许长度,超过了则把链表替换为红黑树。

4.Questions

  1. resize:容量扩容方法
  2. putTreeVal:红黑树添加节点。
  3. treeifyBin:将链表转换为红黑树。
  4. TREEIFY_THRESHOLD :默认链表长度,默认值为8,当链表长度超过时将链表转换为红黑树。
  5. threshold:容量阈值,当容量达到阈值时进行扩容。

猜你喜欢

转载自blog.csdn.net/qq_35321596/article/details/81117669