第二节课HashMap源码分析.md 4.1 KB

分析1.8

核心参数:

  1. HashMap初始容量 static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
  2. HashMap的最大容量 static final int MAXIMUM_CAPACITY = 1 << 30;
  3. 加载因子 16×0.75=12 一旦size大于12提前扩容 static final float DEFAULT_LOAD_FACTOR = 0.75f;
  4. 链表长度大于8,将链表转换成红黑树 static final int TREEIFY_THRESHOLD = 8;
  5. 红黑树的节点个数小于6就将红黑树转换为链表 static final int UNTREEIFY_THRESHOLD = 6;
  6. 数组容量大于64的情况下,将链表转换成红黑树 static final int MIN_TREEIFY_CAPACITY = 64;

底层采用单向链表

final int hash;
final K key;
V value;
Node<K,V> next;

为什么要将key的hash值保存起来? 下次扩容的时候,能够计算该key在新的table中index值

// transient不能被序列化
transient Node<K,V>[] table;
transient int size;
// 遍历hashmap集合的时候,防止多线程篡改我们的数据
transient int modCount;
// 加载因子
final float loadFactor;

分析hashmap的put方法底层实现原理

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        // n是当前table数组的长度,i就是index下标位。table和p临时table大小接受
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        // 将全局table=tab判断是否为空,如果为空的情况下,且长度=0开始对table实现扩容,实现懒加载
        if ((tab = table) == null || (n = tab.length) == 0)
        	// 默认的大小为16
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            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) {
                    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;
    }
  1. n是当前table数组的长度,i就是index下标位。table和p临时table大小接受 Node[] tab; Node p; int n, i;
  2. 将全局table=tab判断是否为空,如果为空的情况下,且长度=0开始对table实现扩容,实现懒加载 if ((tab = table) == null || (n = tab.length) == 0) 默认的大小为16 n = (tab = resize()).length; 获取原来的table容量 int oldCap = (oldTab == null) ? 0 : oldTab.length; 下一次扩容的大小 int oldThr = threshold; 这一次扩容的大小,下一次扩容的阈值 int newCap, newThr = 0;
  3. p就是链表 p = tab[i = (n - 1) & hash]) == null
  4. 如果hash值相等并且equals也相等,直接覆盖 if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p;
  5. 将新的值覆盖 if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; }
  6. 找到该结点 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break;
  7. hashmap线程不安全 防止hashmap线程冲突 添加新元素 ++modCount;-----fastclass机制
  8. 如果size大于12,提前扩容 if (++size > threshold) if (++size > threshold) resize();