HashMap 源码解析之 resize()方法
贴下源码
/**
* 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
*/
翻译下上面这段注释:
初始化或者扩容列表大小两倍。如果列表为空,根据阈值字段分配初始 容量。否则,因为我们采用每次扩容两倍的方式,扩容后每个桶中的元素在新表中索引位置要不不变,要不偏移2的次幂。
final Node<K,V>[] resize() {
// 旧的 table
Node<K,V>[] oldTab = table;
// 旧table的大小,
int oldCap = (oldTab == null) ? 0 : oldTab.length;
// 旧table的阈值
int oldThr = threshold;
// 初始化新table的阈值和大小
int newCap, newThr = 0;
// 旧table的容量大于0
if (oldCap > 0) {
// 如果旧table的大小大于或等于table的最大length
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
// 不在扩容,直接返回旧的table
return oldTab;
}
// 旧table扩容一倍 < 最大容量 同时 旧table的容量大于默认的初始容量
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
// 阈值扩容一倍
newThr = oldThr << 1; // double threshold
}
// 旧的阈值大于0
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else
// 初始化table
{ // 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"})
//使用新的容量大小初始化一个新的table
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
// 赋值给table
table = newTab;
// 旧的table不为null
if (oldTab != null) {
// 循环开始扩容
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
// 从旧的table 中去除一个node
if ((e = oldTab[j]) != null) {
// 将旧table中的元素置为null
oldTab[j] = null;
// 链表中只有一个元素
if (e.next == null)
// 直接赋值
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode) // 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;
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;
// 第二条链表的位置为原来的位置加上旧table的大小
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
可以看到上述方法主要是针对在table扩容的时候处理table中的数据。首先我们可以看到每次扩容的后的容量为之前的两倍。在针对table中每个元素 的处理主要分为普通的node,单项链表和红黑树,
扩容时,对于链表会分为两条新的链表,这样做在后续发生hash碰撞时,避免单条链表长度过长,增加查询时间,影响性能。
当单条链表的长度小于6时,扩容时,会自动将红黑树在转换为链表结构。
treeNode的split方法
先上代码
final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
TreeNode<K,V> b = this;
// Relink into lo and hi lists, preserving order
// 将原来的红黑树分为两条新的树
TreeNode<K,V> loHead = null, loTail = null;
TreeNode<K,V> hiHead = null, hiTail = null;
int lc = 0, hc = 0;
for (TreeNode<K,V> e = b, next; e != null; e = next) {
next = (TreeNode<K,V>)e.next;
e.next = null;
if ((e.hash & bit) == 0) {
// 树中是否有元素
if ((e.prev = loTail) == null)
// 将当前元素置为树的第一个元素
loHead = e;
else
loTail.next = e;
loTail = e;
++lc;
}
else {
if ((e.prev = hiTail) == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
++hc;
}
}
if (loHead != null) {
if (lc <= UNTREEIFY_THRESHOLD)
tab[index] = loHead.untreeify(map);
else {
tab[index] = loHead;
if (hiHead != null) // (else is already treeified)
loHead.treeify(tab);
}
}
if (hiHead != null) {
// 判断时否需要转换为链表
if (hc <= UNTREEIFY_THRESHOLD)
tab[index + bit] = hiHead.untreeify(map);
else {
tab[index + bit] = hiHead;
if (loHead != null)
hiHead.treeify(tab);
}
}
}
看完代码后想到一个问题:为什么hashmap要采用数组加链表的形式来实现?为什么不采用二维数组的方式来实现 呢?
欢迎各位小伙伴一起来交流经验。