HashMap
一、概述
HashMap作为面试的常考题,对一个Java开发者来说是要掌握相关知识点的。以下文章是对HashMap的resize()函数流程理解。
二、算法概述
在了解resize()函数之前可以先考一道算法题(如果不想看,可以直接跳过,进入resize()分析),我发现他们有着类似的实现。题目来自《程序员代码面试指南》。不得不佩服左神🐂🍺
题目描述如下:
给定一个链表,再给定一个整数 pivot,请将链表调整为左部分都是值小于 pivot 的节点,中间部分都是值等于 pivot 的节点, 右边部分都是大于 pivot 的节点。
除此之外,对调整后的节点顺序没有更多要求
pivot = 5
7 -> 9 -> 1 -> 8 -> 5 -> 2 -> 5
output:
1 -> 2 -> 5 -> 5 -> 7 -> 9 -> 8
算法思路
- 将原链表中的所有节点依次划分进三个链表,三个链表分别为small代表左部分,equal代表中间部分,big代表右部分。
small:1 -> 2 -> null
equal:5 -> 5 -> null
big: 7 -> 9 -> 8 null
- 将small、equal和big三个链表重新串起来即可。
三、代码实现
注释1,2,3,容易造成链表环状。
public static ListNode listPartition(ListNode head, int pivot) {
ListNode sH = null, sT = null;
ListNode mH = null, mT = null;
ListNode bH = null, bT = null;
ListNode next = null;
while(head != null) {
next = head.next; // 1
head.next = null; // 2
if (head.val < pivot) {
if(sH == null) {
sH = head;
sT = head;
}else {
sT.next = head;
sT = head;
}
}else if (head.val == pivot) {
if (mH == null) {
mH = head;
mT = head;
}else {
mT.next = head;
mT = head;
}
} else {
if (bH == null) {
bH = head;
bT = head;
}else {
bT.next = head;
bT = head;
}
}
head = next; // 3. head = head.next;
}
if (sT != null) {
sT.next = mH;
mT = mT != null ? mT : sT;
}
if (mT != null) {
mT.next = bH;
}
return sH != null ? sH : mH != null ? mH : bH;
}
四、HashMap的resize()分析
JDK1.8和JDK1.7的HashMap的扩容不太一样,可以参考美团技术文章Java 8系列之重新认识HashMap,以下是扩容大概步骤
- 计算新阈值(newThr)和hash数组大小(newCap)
- 创建新的Hash数组
- 把原来元素放到新的Hash数组里面:把一个链表拆分成2个链表:0号链表、1号链表;0号链表里面的元素的下标不变,1号链表的元素的下标= 原来的下标 + oldCap。如下图所示
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) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
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;
/* 上面代码计算新的阈值和新的hash数组容量**/
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
/* 上面的代码分配新的Hash数组**/
/* 下面的代码把元素转移到新的数组里面**/
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> 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<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;
// 0 号链表
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
// 1 号链表
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
// 尾指针置为null,防止造成环
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
// 尾指针置为null,防止造成环
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
五、总结
之前也是参考好多别人的博客,感觉都是似懂非懂的,今天算是搞明白了,算法还是重要的。以上仅是我个人的理解,如有错误还希望指正