看看HashMap源码

前言

为什么突然想到要“看看HashMap源码”?当然是因为爱学习!算了,不装13了。其实主要原因是,之前在学习高并发的时候,我们知道HashMap在高并发环境是不安全的,而且有可能使cpu瘫痪。看到这里,我已经很感兴趣了,这玩意还能瘫痪cpu啊。然后了解到的原因是在高并发环境下,HashMap的链表有可能成环,那么get()操作会死循环。为什么会成环呢?如何才能成环呢?抱着这个想法就想”看看HashMap源码”。但是,写到一半突然被告知jdk8已经修复了这个bug。

成员变量

进入Hashmap的源码(jdk1.8.0_91),还是熟悉的配方熟悉的味道,那就是注释比代码多,这些注释就是我们学习源码的辅导资料。首先看看,Hashmap类的成员变量(没有列举全部,只是挑选了常用的)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
transient Node<K,V>[] table;
/**
* The number of key-value mappings contained in this map.
*/
transient int size;
/**
* The number of times this HashMap has been structurally modified
* Structural modifications are those that change the number of mappings in
* the HashMap or otherwise modify its internal structure (e.g.,
* rehash). This field is used to make iterators on Collection-views of
* the HashMap fail-fast. (See ConcurrentModificationException).
*/
transient int modCount;
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
* The load factor used when none specified in constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* The bin count threshold for using a tree rather than list for a
* bin. Bins are converted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
*/
static final int TREEIFY_THRESHOLD = 8;

其实源码的注释已经介绍的很清楚了,这里只是重复。
table,一个存放节点(Node[])的数组,是Hashmap的基础设施,所有的节点都存放于此。
size,Hashmap存放的键值对的数目,并不等于table数组的长度,因为可能存在链表和红黑树结构。
modCount,Hashmap的修改次数,是实现fail-fast机制的关键(关于fail-fast可以参考ConcurrentModificationException)。
DEFAULT_INITIAL_CAPACITY,表示默认HashMap数组初始大小为16,并且为了后续的rehash操作的方便,Hashmap的数组大小始终为2的整数次幂,即使你输入一个不是2的整数次幂的值,也会变成最小的大于该值的2的整数次幂。
MAXIMUM_CAPACITY,表示Hashmap数组的最大容量,初始值为2^30。
DEFAULT_LOAD_FACTOR,表示负载因子,当Hashmap的实际容量超过了(设定容量x负载因子),就触发rehash操作,默认值为0.75。
TREEIFY_THRESHOLD,jkd1.8新增的,如果Hashmap数组元素的链表长度超过这个值,就使用红黑树结构代替链表提高查询效率,默认值为8。

在看看Hashmap的数组中存放的数据结构到底是如何定义的(截取部分源码):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Basic hash bin node, used for most entries. (See below for
* TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
*/
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
...
}

数组内的元素的数据结构继承了Map.Entry,用于存放键值对,另外还包含了hash值和next节点,其中hash值可用于存取节点时来寻址的作用,next节点是实现Hashmap的数组+链表(红黑树)结构的关键。

在继续看Hashmap的内部方法之前,做个大致的总结:

Hashmap是用于存放键值对的容器,内部实现是基于数组的,数组中存放的是键值对Node节点,一个Node节点保存了一个键值对信息,同时还保存了next节点,可以形成链表结构(在发生hash冲突的时候)。如果链表长度太长,超过了阀值(默认为8),那么就自动升级为红黑树结构(高效的平衡查找树),这样一来,数组元素的节点就成为了红黑树的根节点了。

Hashmap的结构如图所示(同时显示了链表和红黑树):

方法

主要介绍put(),get(),resize(),弄清楚Hashmap内部的工作流程即可。

put()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}

注释中说明了如果put()方法添加的键值对的键已经存在于Hashmap中,那么就用新的键值的值替代旧值。再看源码put()调用了hash()方法以及putVal()方法。我们先看hash()方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Computes key.hashCode() and spreads (XORs) higher bits of hash
* to lower. Because the table uses power-of-two masking, sets of
* hashes that vary only in bits above the current mask will
* always collide. (Among known examples are sets of Float keys
* holding consecutive whole numbers in small tables.) So we
* apply a transform that spreads the impact of higher bits
* downward. There is a tradeoff between speed, utility, and
* quality of bit-spreading. Because many common sets of hashes
* are already reasonably distributed (so don't benefit from
* spreading), and because we use trees to handle large sets of
* collisions in bins, we just XOR some shifted bits in the
* cheapest possible way to reduce systematic lossage, as well as
* to incorporate impact of the highest bits that would otherwise
* never be used in index calculations because of table bounds.
*/
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

当输入的key为null时,hash值为0,也就是说Hashmap的key是可以为null的。对比HashTable,HashTable的key直接进行了hashCode,如果key为null时,会抛出异常,所以HashTable的key不可以是null。
具体如何得到key的hash值呢?首先调用key自身的hashcode()得到一个hash值h(32位int类型),然后将h与h右移16位之后的数进行异或,得到最终的hash值。至于为什么这么做,这是前人总结出来的算法可以使得hash值分布更加均匀,尽量减少冲突。

再来看看putVal()方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Implements Map.put and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}

通过注释,我们可以知道入参都代表了什么:

  • hash:表示key的hash值
  • key:待存储的key值
  • value:待存储的value值
  • onlyIfAbsent:是否需要替换相同的value值。如果为true,表示不替换已经存在的value
  • evict:如果为false,表示数组是新增模式(暂时不知道啥意思,只在方法的最后出现,但不影响其他逻辑)

分解来看,上述方法都做了些什么。

首先判断当前HashMap的数组是否为空,如果为空,就调用resize()方法初始化一个长度为16的数组,并且获取到数组的长度n,代码如下:

1
2
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;

然后,根据数组的长度n-1的值与入参key的hash值按位与运算,算出hash值对应于数组中的位置,从tab中将这个位置上面的内容取出,判断为null时,在这个位置新增一个Node。但是,如果取到了数据,也就是这个hash值对应数组的位置上面已经有了键值对存在。那么,就判断这个Node(原先数组中的Node),也就是p的hash值是否与传入的hash相等,然后接着判断key是否相等(这里判断key是否相等,用了一个或运算)。如果判断通过,表示要传入的key-val键值对就是tab[i]位置上面的键值对,直接替换即可,不用管后面是链表还是红黑树。如果不是的话,就将这个新的键值对插入链表或者红黑树中即可。
插入键值对分两种情况:如果数组元素是链表时,就将节点新增到列表头部。如果链表的长度大于等于红黑树化的阈值-1,就将链表转成红黑树。如果数组元素是红黑树的话,就直接插入键值对Node即可。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}

最后,将修改次数加一,同时判断当前的键值对数量是否即将超过阈值,如果即将超过,需要进行resize()操作。

1
2
3
4
5
6
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;

get()

介绍完put()方法,get()就相对容易理解了。还是看源码吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
*
* <p>A return value of {@code null} does not <i>necessarily</i>
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @see #put(Object, Object)
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}

根据入参的key对象计算出key的hash值,调用getNode()方法,再来看看getNode()方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Implements Map.get and related methods
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}

通过key的hash值与key对象,来查找key对应的键值对的值,如果查找失败则返回null。如何查找的呢?首先,通过key的hash值计算出对应数组的索引,如果索引到的第一个Node节点的key和hash值与入参相等,直接返回该Node。否则,循环遍历下一个节点(可能是链表也有可能是红黑树)。

resize()

在学习源码之前,先看看resize()方法的注释。resize()方法的注释写的很概括,基本介绍了该方法的作用与特点。

1
2
3
4
5
6
7
8
9
10
11
/**
* 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() {...}

在resize的时候,数组容量还是要保持为2的整数次幂,所以扩容的时候容量会翻倍(原容量乘以2),那么在resize的时候原来的元素在新数组中要不就维持原索引,要不就从原位置再移动2次幂,下文的源码中会有详解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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) {
//如果老的数组容量大于0,首先判断是否大于等于HashMap的最大容量。如果true,将阈值设置为Integer的最大值,同时数组容量不变
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;
@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;
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;
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;
}

为了方便起见,一些简单的逻辑我直接就在源码中注释了。这里介绍Hashmap在resize()的时候,如何将原数组的元素复制到新数组中去的。注释中说到“the elements from each bin must either stay at same index, or move with a power of two offset in the new table.”也就是说,元素的位置要么是在原位置,要么是在原位置再移动2次幂的位置。如何实现的呢?

这里假设一种情况,原数组容量n为16,那么n-1的二进制码:0000 0000 0000 0000 0000 0000 0000 1111 。
hash(key1):1111 1111 0000 0000 1111 0000 1111 0101
hash(key1):1111 1111 0000 0000 1111 0000 1110 0101
由此可见,(hash(key1))&(n-1) = (hash(key2))&(n-1) = 0101 。说明,key1,key2在容量为16的数组中的索引位置相同,在同一个链表(红黑树)中。
现在,原数组容量翻倍n=32,n-1的二进制码:0000 0000 0000 0000 0000 0000 0001 1111 。
那么,(hash(key1))&(n-1) = 1 0101;(hash(key2))&(n-1) = 0 0101
所以,key2在新数组中的位置与原来保持一致,而key1的位置则是原来位置+16。

这样设计的好处在于我们在扩充HashMap的时候,不需要重新计算hash的值,只需要看看原来的hash值新增的那个二进制位是1还是0就好了,是0的话索引没变,是1的话索引变成“原索引+原数组容量”。这样不但省去了重新计算hash值的时间,而且由于新增的二进制位是0是1可以认为随机,也就把原数组中的hash冲突均匀的分散了。这也解释了为什么Hashmap的容量必须是2的整数次幂了。

总结

以上就是HashMap中比较重要的源码的简要分析,最后需要注意的是在高并发的情况下,还是尽量使用ConcurrentHashMap,因为Hashmap不是线程安全的,而Hashtable通过给整个map加锁的方式性能不好。

0%