1、HashMap的扩容?
HashMap中的loadFactor负载因子,就是用来进行扩容的。当数组长度 > 阀值(阀值 = 数组长度 * 负载因子)的时候,就会进行HashMap的扩容。
2、JDK1.7的HashMap扩容?
取出数组中的元素,然后遍历以该元素为头的单向链表元素,根据每一个被遍历元素的hash值来计算在新数组中的新下标。
3、JDK1.7扩容源代码?
//扩容的长度是原来的长度*2
void resize(int newCapacity) {
//先将原来的值赋值给oldTable
Entry[] oldTable = table;
//老的长度
int oldCapacity = oldTable.length;
//如果旧的长度等于最大的长度,就直接用最新的
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
//根据新的长度创建好的newTable
Entry[] newTable = new Entry[newCapacity];
//initHashSeedAsNeeded(newCapacity)这个返回值为false
//转移
transfer(newTable, initHashSeedAsNeeded(newCapacity));
table = newTable;
//计算容量*负载因子
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
//遍历原来的数组table
for (Entry<K,V> e : table) {
//遍历数组下面的链表
while(null != e) {
Entry<K,V> next = e.next;
//不需要重新rehash
if (rehash) {
e.hash = null == e.key ? 0 : hash(e.key);
}
//(关键)在新数组中,计算出新的位置
int i = indexFor(e.hash, newCapacity);
//放入到对应位置
e.next = newTable[i];
newTable[i] = e;
e = next;
}
}
}
4、JDK1.7中的死循环问题?
死循环详解:HashMap死循环问题_DreamMakers的博客-CSDN博客_hashmap死循环
遍历到链表尾部的时候,因为头插法的原因,尾部链表会指向头部链表,造成死循环。
这也是JDK1.8中改成尾插法的其中一个原因。
5、JDK1.8的扩容源代码?
/**
* 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.
*
* @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;
if (oldCap > 0) {
//超过数据在java中的最大容量就只能接收冲突了
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//否则,就将长度和柔亮扩大为原来的2倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
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;
if (oldTab != null) {
//遍历老数组下标索引
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
//如果老数组的对应索引上有元素,就取出链表头存放在e中
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
//如果j下标对应的位置只有一个元素,那么就直接计算它在新数组中的位置
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
//如果是树结构,调用split方法处理
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
//链表结构的冲突
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
//循环处理链表冲突上的每一个元素
do {
next = e.next;
//key的hash值和老数组长度进行与操作,判断元素是放在原索引位置还是新索引位置
//放在原索引处,建立新链表
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;
}
if (hiTail != null) {
//放入新索引
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
6、JDK1.8源码中的e.hash & oldCap?
在1.8的扩容中,元素添加有两种情况。一种是直接添加在原索引的链表后面,第二种是添加在(原索引 + 老数组长度)的位置链表后面。而两种情况是根据e.hash & oldCap的计算结果来决定的。
假设,当前HashMap容量为8 = 1000,hash1 = 5 = 0101(元素1),hash2 = 13 = 1101(元素2)。当容量从8扩容到16的时候,分别通过e.hash & oldCap对这两个元素重新计算索引位置。
int oldCap = 8, newCap = 16;
int hash1 = 5, hash2 = 13;
int oldIndex1 = hash1 & (oldCap - 1); //0101 & 0111 = 0101 结果为5
int oldIndex2 = hash2 & (oldCap - 1); //1101 & 0111 = 0101 结果为5
//以上是两个元素在旧数组中的索引位置,均为5
//那么在新数组中的新位置呢
int newIndex1 = hash1 & (newCap - 1); //0101 & 01111 = 00101 结果为5
int newIndex2 = hash2 & (newCap - 1); //1101 & 01111 = 01101 结果为13
//通过以上索引计算的结果,元素1保持原位置,元素2在新的索引位置
//那么通过HashMap扩容中使用的方法如何判断呢?
//根据上述源码
int judge1 = hash1 & oldCap; //0101 & 1000 = 0000 结果为0
int judge2 = hash2 & oldCap; //1101 & 1000 = 1000 结果为8 非0结果
总结:通过e.hash & oldCap的与操作结果是否为0来判断,元素在新数组中的索引位置。
7、JDK1.7和JDK1.8的改动?
不同处 | JDK1.7 | JDK1.8 |
存储结构 | 数组+链表 | 数组+链表/红黑树 |
初始化方式 | 单独函数:inflateTable() | 全都集成到resize()方法中 |
hash值的计算方式 | 扰动处理 = 9次扰动 = 4次位运算 + 5次异或运算 | 扰动处理 = 2次扰动 = 1次位运算 + 1次异或运算 |
存放数据的规则 | 当元素没有冲突的时候,存放到数组里面; 元素之间发生冲突,存放到链表中; | 当元素没有冲突的时候,存放到数组里面; 元素之间发生冲突,存放到链表中;而链表长度>8 && 哈希数组长度 > 64的时候,将链表进行树化; |
元素插入方式 | 头插法(多线程死循环问题) | 尾插法 |
扩容后如何计算下标 | 按照原本的方式,计算hashCode——>扰动处理——>hash & (length - 1)。在新数组中进行重新的下标计算。 | 索引位置只有两种情况,核心通过e.hash & oldCap来判断。 (1)原索引位置 (2)原索引位置+旧数组长度 |
参考
HashMap 在扩容时为什么通过位运算 (e.hash & oldCap) 得到新数组下标_Luke.Du的博客-CSDN博客_hashmap扩容后数组下标会改变吗
HashMap扩容时的rehash方法中(e.hash & oldCap) == 0算法推导_Dylanu的博客-CSDN博客_e.hash