1、HashMap采用的是数据+链表的存储结构
2、初始化默认长度为16,每次扩容*2,负载因子默认0.75
3、
扩容核心类
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {
while(null != e) {
Entry<K,V> next = e.next;
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;
}
}
}
上面的方法中,采用数组循环嵌套链表遍历的方式将每个元素对应到新数组中的指定位置的链表中
Entry<K,V> next = e.next;
.
.
.
e = next;
这个很明显是链表遍历下一个元素
int i = indexFor(e.hash, newCapacity);//根据hash值重新定位当前元素在新数组中的下标位置
接下来这两句乍一看比较难理解,其实说开了就比较简单了,它是将新元素插入到链表的表头而已
e.next = newTable[i];
newTable[i] = e;