共计 5267 个字符,预计需要花费 14 分钟才能阅读完成。
jdk1.8 的 hashMap 相比 1.7 的最大的变动就是结构的修改,在之前数组 + 链表的基础上,增加了红黑树的结构。
1.7 的 hashMap 我们已经看过了,其中在查找节点的时候,会去根据 hash 找到对应的数组,接着去遍历之后的链表结构,当 hash 冲突比较多的时候,链表就会非常的长,此时遍历链表的效率就会很低,所以大神们在将红黑树加入到了 1.8 的 hashMap 中,当链表长度大于 8 的时候,会将链表转换为红黑树,提高了查找节点的效率。如下图:
下面我们就来一起学习一下 jdk1.8 的 hashMap 源码吧!
这里我想先吐槽一下 1.8 的源码,代码可读性比 1.7 差太多了 … 脑壳疼~ 但是在精简程度上要比 1.7 的好一些。
先看 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) {// 这里 p 可以视为一个指针,指向 tab[i]位置的节点 | |
// n: 数组的 length | |
// i: 根据 hash 算出的数组下标 | |
Node<K,V>[] tab; Node<K,V> p; int n, i; | |
// 将 tab 数组指向 table,并判断 table 如果为空,则进入 resize()中进行初始化 | |
if ((tab = table) == null || (n = tab.length) == 0) | |
// 1.8 的 hashMap 将初始化方法和 resize()合并到了一起 | |
n = (tab = resize()).length; | |
// 很据 hash 值找到 tab[i]并将 p 指向 tabl[i],如果没有内容,创建新的链表节点放到 i 的位置 | |
if ((p = tab[i = (n - 1) & hash]) == null) | |
tab[i] = newNode(hash, key, value, | |
else {// 进入 else 表示 tabl[i]处有内容,下面需要进一步判断 key 是否一致 | |
Node<K,V> e; K k; | |
// 插入的 key 和 tab[i]处的 key 相等,将 p 赋值给 e(exist)节点 | |
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 {// 进入 else 说明 hash 相同,且 tab[i]处是一个多节点的链表 | |
// 循环链表 | |
for (int binCount = 0; ; ++binCount) { | |
// 将 e 指向 p.next,并判断 p.next 是否有内容,// 如果没有内容,说明 tab[i]处没找到一致的 key,将会此节点作为新节点插入 | |
// 插入的位置为链表尾部 | |
if ((e = p.next) == null) {p.next = newNode(hash, key, value, null); | |
// 链表节点数如果大于 8,调用 treeifyBin 将链表转为红黑树 | |
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for | |
treeifyBin(tab, hash); | |
break; | |
} | |
// 在 tab[i]中找到了相同的 key,跳出循环 | |
// 此时 e 指向 tab[i]中 key 等于新插入 key 的链表节点 | |
if (e.hash == hash && | |
((k = e.key) == key || (key != null && key.equals(k)))) | |
break; | |
p = e; | |
} | |
} | |
// 插入的 key 在链表中已存在,只需要直接覆盖即可 | |
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; | |
} |
还需要注意的是
- 1.8 中的 put 方法是在链表结尾插入新节点,而 1.7 是在头部插入新节点
- 1.8 是先插入,再扩容,1.7 是先扩容,再插入
接下来看一下 resize()
扩容方法
final Node<K,V>[] resize() {Node<K,V>[] oldTab = table; | |
// 当前容量 | |
int oldCap = (oldTab == null) ? 0 : oldTab.length; | |
// 当前阈值 | |
int oldThr = threshold; | |
int newCap, newThr = 0; | |
// 当前容量 >0,表示 map 中已有内容 | |
if (oldCap > 0) {if (oldCap >= MAXIMUM_CAPACITY) { | |
threshold = Integer.MAX_VALUE; | |
return oldTab; | |
} | |
// 扩容一倍,并将阈值×2 | |
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && | |
oldCap >= DEFAULT_INITIAL_CAPACITY) | |
newThr = oldThr << 1; // double threshold | |
} | |
// 首次 put,oldThr > 0 表示使用的是 `new HashMap(int initialCapacity)` 构造器进行的初始化 | |
else if (oldThr > 0) // initial capacity was placed in threshold | |
// 初始化大小 = 阈值 | |
newCap = oldThr; | |
// 首次 put,else 表示使用的是默认构造器 `new HashMap()` 进行的初始化 | |
else { // zero initial threshold signifies using defaults | |
// 初始化大小 = 默认大小 (16) 并计算阈值 | |
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; | |
// oldTab[j]处只有一个节点,就不需要遍历链表了,直接将此节点赋值到新数组对应 hash 位置上 | |
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; | |
// e.hash & oldCap 将旧的链表分成了 lo(e.hash & oldCap 为偶数)和 hi(e.hash & oldCap 为奇数)两个链 | |
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); | |
// lo 链会分配到和原下标相同的位置 | |
// hi 链会被分配到原下标 +oldCap 的位置 | |
if (loTail != null) { | |
loTail.next = null; | |
// lo 链 | |
newTab[j] = loHead; | |
} | |
if (hiTail != null) { | |
hiTail.next = null; | |
// hi 链 | |
newTab[j + oldCap] = hiHead; | |
} | |
} | |
} | |
} | |
} | |
return newTab; | |
} |
get()
方法
相对 put 方法,get 就简单了许多
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; | |
} |
remove()
删除节点
final Node<K,V> removeNode(int hash, Object key, Object value, | |
boolean matchValue, boolean movable) {Node<K,V>[] tab; Node<K,V> p; int n, index; | |
if ((tab = table) != null && (n = tab.length) > 0 && | |
(p = tab[index = (n - 1) & hash]) != null) { | |
// node: 要被删除的节点 | |
Node<K,V> node = null, e; K k; V v; | |
// 如果头节点匹配,直接将 node 指向头节点 | |
if (p.hash == hash && | |
((k = p.key) == key || (key != null && key.equals(k)))) | |
node = p; | |
else if ((e = p.next) != null) { | |
// 如果头节点不匹配,且头节点属于红黑树节点,从树中取出要删除的节点 | |
if (p instanceof TreeNode) | |
node = ((TreeNode<K,V>)p).getTreeNode(hash, key); | |
// 如果头节点不匹配,且头节点属于链表节点,遍历链表取出要删除的节点 | |
else { | |
do { | |
if (e.hash == hash && | |
((k = e.key) == key || | |
(key != null && key.equals(k)))) { | |
node = e; | |
break; | |
} | |
p = e; | |
} while ((e = e.next) != null); | |
} | |
} | |
// 如果找到了符合条件的待删除节点,根据节点类型去红黑树中或者链表中删除指定节点 | |
if (node != null && (!matchValue || (v = node.value) == value || | |
(value != null && value.equals(v)))) {if (node instanceof TreeNode) | |
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable); | |
else if (node == p) | |
tab[index] = node.next; | |
else | |
p.next = node.next; | |
++modCount; | |
--size; | |
afterNodeRemoval(node); | |
return node; | |
} | |
} | |
return null; | |
} |
总结一下:
- 相对 1.7 的 hashmap 不同,1.8 的结构采用数组 + 链表 + 红黑树的结构
- 1.8 中的 put 方法是在链表结尾插入新节点,而 1.7 是在头部插入新节点
- 1.8 是先插入,再扩容,1.7 是先扩容,再插入
至此,1.8 的 hashMap 源码阅读到这里就告一段落了,后边我们会继续看一下不同版本的 ConcurrentHashMap
源码,欢迎观看~
正文完