最近看了下jdk8的hashmap源码,相比于7,在存储结构上有了些改变。
1.在jdk8之前,hashmap的存储结构是数组+链表的形式,那么这种方式随着链表的长度增加,效率也凸显出来。
所以在jdk8中这块做了优化,当链表超过一定长度时转化为红黑树来解决这个问题,下面用流程图画出hashmap
的put方法实现逻辑。
下面请带着这些问题看源码,为什么树的查找效率比链表高,达到什么样的条件会扩容,为什么扩容会影响效率
呢,怎样实现链表到树的转化?
2.先来看下put方法源码。
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果table为空,则创建一个table
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//确定插入table的位置,算法是(n – 1) & hash
if ((p = tab[i = (n – 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//在table的i位置发生碰撞,有两种情况,1、key值是一样的,替换value值,
//2、key值不一样的有两种处理方式:2.1、存储在i位置的链表;2.2、存储在红黑树中
else {
Node<K,V> e; K k;
// 如果hash、key都相等,直接覆盖
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//2.2
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//2.1
else {
//开始遍历链表
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//如果冲突节点超过8调用treeifyBin转化成红黑树
if (binCount >= TREEIFY_THRESHOLD – 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//如果链表上的值出现hash,key和待插入的值相等,则退出循环。
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;//将p调整为下一个节点
}
}
//如果e非空就替换旧的oldValue值
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//threshold=newThr:(int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); //默认0.75*16,大于threshold值就扩容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
1 final
Node<K,V>[] resize() {
2
Node<K,V>[] oldTab = table;
3
int
oldCap = (oldTab ==
null
) ?
0
: oldTab.length;
4
int
oldThr = threshold;
5
int
newCap, newThr =
0
;
6
if
(oldCap >
0
) {
7
// 超过最大值就不再扩充了,就只好随你碰撞去吧
8
if
(oldCap >= MAXIMUM_CAPACITY) {
9
threshold = Integer.MAX_VALUE;
10
return
oldTab;
11
}
12
// 没超过最大值,就扩充为原来的2倍
13
else
if
((newCap = oldCap <<
1
) < MAXIMUM_CAPACITY &&
14
oldCap >= DEFAULT_INITIAL_CAPACITY)
15
newThr = oldThr <<
1
;
// double threshold
16
}
17
else
if
(oldThr >
0
)
// initial capacity was placed in threshold
18
newCap = oldThr;
19
else
{
// zero initial threshold signifies using defaults
20
newCap = DEFAULT_INITIAL_CAPACITY;
21
newThr = (
int
)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
22
}
23
// 计算新的resize上限
24
if
(newThr ==
0
) {
25
26
float
ft = (
float
)newCap * loadFactor;
27
newThr = (newCap < MAXIMUM_CAPACITY && ft < (
float
)MAXIMUM_CAPACITY ?
28
(
int
)ft : Integer.MAX_VALUE);
29
}
30
threshold = newThr;
31
@SuppressWarnings
({
“rawtypes”
,
“unchecked”
})
32
Node<K,V>[] newTab = (Node<K,V>[])
new
Node[newCap];
33
table = newTab;
34
if
(oldTab !=
null
) {
35
// 把每个bucket都移动到新的buckets中
36
for
(
int
j =
0
; j < oldCap; ++j) {
37
Node<K,V> e;
38
if
((e = oldTab[j]) !=
null
) {
39
oldTab[j] =
null
;
40
if
(e.next ==
null
)
41
newTab[e.hash & (newCap –
1
)] = e;
42
else
if
(e
instanceof
TreeNode)
43
((TreeNode<K,V>)e).split(
this
, newTab, j, oldCap);
44
else
{
// 链表优化重hash的代码块
45
Node<K,V> loHead =
null
, loTail =
null
;
46
Node<K,V> hiHead =
null
, hiTail =
null
;
47
Node<K,V> next;
48
do
{
49
next = e.next;
50
// 原索引
51
if
((e.hash & oldCap) ==
0
) {
52
if
(loTail ==
null
)
53
loHead = e;
54
else
55
loTail.next = e;
56
loTail = e;
57
}
58
// 原索引+oldCap
59
else
{
60
if
(hiTail ==
null
)
61
hiHead = e;
62
else
63
hiTail.next = e;
64
hiTail = e;
65
}
66
}
while
((e = next) !=
null
);
67
// 原索引放到bucket里
68
if
(loTail !=
null
) {
69
loTail.next =
null
;
70
newTab[j] = loHead;
71
}
72
// 原索引+oldCap放到bucket里
73
if
(hiTail !=
null
) {
74
hiTail.next =
null
;
75
newTab[j + oldCap] = hiHead;
76
}
77
}
78
}
79
}
80
}
81
return
newTab;
82
}