## 分析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; 底层采用单向链表 ```java final int hash; final K key; V value; Node next; ``` 为什么要将key的hash值保存起来? 下次扩容的时候,能够计算该key在新的table中index值 ```java // transient不能被序列化 transient Node[] table; transient int size; // 遍历hashmap集合的时候,防止多线程篡改我们的数据 transient int modCount; // 加载因子 final float loadFactor; ``` ## 分析hashmap的put方法底层实现原理 ```java final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { // n是当前table数组的长度,i就是index下标位。table和p临时table大小接受 Node[] tab; Node 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 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)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(); ## 分析hashmap扩容原理 扩容的时候以2的幂次方扩容,将原来的table中存放的key循环遍历存入新的table中 误区:不会重新计算hash值,重新计算index(为什么链表会缓存hash值) ### 1.7版本的扩容 ```java void transfer(Entry[] newTable, boolean rehash) {     int newCapacity = newTable.length;     // 遍历原来宿主中所有的链表     for (Entry e : table) {     // 如果存在链表       while(null != e) {         Entry next = e.next;         // 是否需要重新计算hash         if (rehash) {           e.hash = null == e.key ? 0 : hash(e.key);         }         // 通过key值的hash值和新数组的大小算出在当前数组中的存放位置         int i = indexFor(e.hash, newCapacity);         // 会发生死循环         // 这里使用了头插法         e.next = newTable[i];         newTable[i] = e;         e = next;       }     } } ``` hashmap1.7 16×2=32 hashmap1.8 16<<1=32 线程安全问题,多线程访问共享全局变量 什么情况下产生死循环? 因为table多线程共享,newTable是线程私有的。会导致C->A->C 如果查询当前不存在的值会导致死循环,导致CPU飙升。 为什么JDK官方不承认JDK1.7的BUG? 多线程才会引起BUG,单线程不会有BUG 多线程推荐使用并发的多线程hashmap ### 1.8版本的扩容 ```java // 查找所有链表 for (int j = 0; j < oldCap; ++j) { Node e; // 如果有链表 if ((e = oldTab[j]) != null) { // 清空原来的链表,也为了避免死循环 oldTab[j] = null; // 判断链表是否到底部 // 新的值肯定比旧值大 if (e.next == null) newTab[e.hash & (newCap - 1)] = e; // 判断当前链表是否为红黑树 else if (e instanceof TreeNode) ((TreeNode)e).split(this, newTab, j, oldCap); else { // preserve order // 如果为链表的情况下 // 低位链表 Node loHead = null, loTail = null; // 高位链表 Node hiHead = null, hiTail = null; Node next; // 遍历当前链表 do { next = e.next; // 拆分成两个链表 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); if (loTail != null) { loTail.next = null; newTab[j] = loHead; } // 这里肯定不会有冲突 // 新的值一定比旧的值大oldCap if (hiTail != null) { hiTail.next = null; newTab[j + oldCap] = hiHead; } } ``` 1.8解决死循环问题? 将一个链表通过与运算拆分成两个链表存放在新的table中。 ## 为什么加载因子不是1,而是0.75? 如果加载因子越大,空间利用率越高,冲突发生概率较大。反之,加载因子越小,空间利用率越低,冲突发生概率较小。综合泊松分布,得出0.75效率最高 ## 如何存放一万条key效率最高? 会触发10次扩容! 推荐指定hashmap集合的初始化容量,(initialCapacity) = 存储元素个数 / 加载因子 + 1 ## hashmap7与8的区别 1. 1.7基于数组加链表,采用头插法 2. 1.8基于数组加链表加红黑树,采用尾插法 A. 能够降低key对应的index冲突概率,提高查询概率 B. 会拆分链表