HashMap源碼解析
1 概述
HashMap是映射類容器,key和value都允許為null,它實作了AbstractMap,Map,Cloneable,Serializable接口等
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
private static final long serialVersionUID = 362498820763181265L;
在jdk1.8之前,他的底層結構是數組加連結清單,連結清單是為了解決哈希沖突而設立的,在jdk1.8之後,有所改變,當滿足以下兩個條件:
- 連結清單容量大于8
- 數組容量大于64
會執行連結清單轉化為紅黑樹的操作。
2 源碼
2.1 屬性
HashMap的屬性及其含義如下:
//預設的初始化容量 為16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
/**
* 預設的最大容量
*/
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;
/**
* The bin count threshold for untreeifying a (split) bin during a
* resize operation. Should be less than TREEIFY_THRESHOLD, and at
* most 6 to mesh with shrinkage detection under removal.
* 将紅黑樹轉化為連結清單的臨界值
*/
static final int UNTREEIFY_THRESHOLD = 6;
/**
* The smallest table capacity for which bins may be treeified.
* (Otherwise the table is resized if too many nodes in a bin.)
* Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
* between resizing and treeification thresholds.
* 數組長度超過此值才能将連結清單轉化為紅黑樹
*/
static final int MIN_TREEIFY_CAPACITY = 64;
//存儲元素的數組,數組的大小總是2的n次方
transient Node<K,V>[] table;
/**
* Holds cached entrySet(). Note that AbstractMap fields are used
* for keySet() and values().
*/
transient Set<Map.Entry<K,V>> entrySet;
/**
* 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).
* 每次擴容和更改map結構的計數器
*/
transient int modCount;
/**
* The next size value at which to resize (capacity * load factor).
*
* @serial
*/
// (The javadoc description is true upon serialization.
// Additionally, if the table array has not been allocated, this
// field holds the initial array capacity, or zero signifying
// DEFAULT_INITIAL_CAPACITY.)
// 臨界值,當實際容量 cap=(r容量*填充因子) 大于此值時,則進行擴容
int threshold;
/**
* The load factor for the hash table.
*
* @serial
* 加載因子
*/
final float loadFactor;
2.2 靜态内部類Node
//此類的作用是用于存儲鍵值對資訊 繼承自Entry
static class Node<K,V> implements Map.Entry<K,V> {
//每個節點都有自己的hash值,用做索引
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;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
//重寫hashcode()方法,以key和value兩個值來計算hashcode
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
//設定value,并傳回oldValue
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
//重寫equals方法
public final boolean equals(Object o) {
if (o == this)
return true;
//如果o是Entry類
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
//key與value都相等
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
2.3 擾動函數
HashMap通過key的hashcode方法經過擾動函數計算hash值,然後通過(hash)&(n-1)判斷目前元素存在的位置
jdk1.8的擾動函數
static final int hash(Object key) {
int h;
//^ 按位異或
//>>> 無符号右移16位,忽略符号位,空位以0補齊
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
jdk1.7的擾動函數
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
jdk1.7的方法的性能更差一些,因為擾動了4次,而jdk1.8隻擾動了一次。
2.4 擴容(重點)
final Node<K,V>[] resize() {
// old tab
Node<K,V>[] oldTab = table;
//old cap
int oldCap = (oldTab == null) ? 0 : oldTab.length;
//pld thr
int oldThr = threshold;
int newCap, newThr = 0;
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
}
//如果表是空的,且有了門檻值,那就代表初始化時指定了門檻值和容量
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) {
//舊表指向null,gc
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
//因為擴容是容量翻倍,是以原連結清單上的節點,有可能繼續存放在原來的下标,或者擴容後的下标,即low+cap 位置
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
//利用位運算,利用哈希值 與 舊的容量,可以得到哈希值去模後,是大于等于oldCap還是小于oldCap,等于0代表小于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);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
2.5 Put方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
接下來看一下putVal方法的實作
//hash傳入的是擾動之後的鍵的哈希值
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果目前表是空的話,進行resize
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;
//如果目前首節點與元素key的hash值與equals都相等的話,直接進行覆寫
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);
//如果現在的連結清單容量已經超過了7,執行treeifBin方法,後面介紹
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;
}
}
//如果e不是null,說明有需要進行覆寫的節點
if (e != null) { // existing mapping for key
V oldValue = e.value;
//如果onlyIfAbsent為true,則不進行覆寫
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//如果容量大于門檻值,進行擴容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
putMapEntries方法
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
int s = m.size();
if (s > 0) {
if (table == null) { // pre-size
//求出目前表的容量
float ft = ((float)s / loadFactor) + 1.0F;
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
//如果目前表的容量大于門檻值,求出目前的門檻值
if (t > threshold)
threshold = tableSizeFor(t);
}
else if (s > threshold)
resize();
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
K key = e.getKey();
V value = e.getValue();
putVal(hash(key), key, value, false, evict);
}
}
}
//根據cap求出實際的容量,必須為2的n次方,便于通過位運算進行取餘操作
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
接下來看一下treeifyBin 方法
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
//如果目前表為null或者目前表的長度小于64,進行擴容
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
//否則的話執行紅黑樹操作了,暫不讨論
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode<K,V> hd = null, tl = null;
do {
TreeNode<K,V> p = replacementTreeNode(e, null);
if (tl == null)
hd = p;
else {
p.prev = tl;
tl.next = p;
}
tl = p;
} while ((e = e.next) != null);
if ((tab[index] = hd) != null)
hd.treeify(tab);
}
}
這張流程圖有兩個問題:
- 長度大于8時,隻有當數組長度大于64才會轉化為紅黑樹
- 直接覆寫之後就會直接return,不會執行++size
2.6 remove方法
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
removeNode方法如下
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<K,V> node = null, e; K k; V v;
//如果首節點就為目标節點
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p; // 把值賦給node
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);
}
}
//如果找到了目标節點且value值也相等
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);
// 如果node為首節點
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size; //容量減一
afterNodeRemoval(node);
return node;
}
}
return null;
}
2.7 get方法
同理,不再贅述
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
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;
}
2.8 周遊方法
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es;
return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}
final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public final int size() { return size; }
public final void clear() { HashMap.this.clear(); }
//一般我們用到entrySet,一般是為了擷取iterator
public final Iterator<Map.Entry<K,V>> iterator() {
return new EntryIterator();
}
public final boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Node<K,V> candidate = getNode(hash(key), key);
return candidate != null && candidate.equals(e);
}
public final boolean remove(Object o) {
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>) o;
Object key = e.getKey();
Object value = e.getValue();
return removeNode(hash(key), key, value, true, true) != null;
}
return false;
}
public final Spliterator<Map.Entry<K,V>> spliterator() {
return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0);
}
public final void forEach(Consumer<? super Map.Entry<K,V>> action) {
Node<K,V>[] tab;
if (action == null)
throw new NullPointerException();
if (size > 0 && (tab = table) != null) {
int mc = modCount;
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next)
action.accept(e);
}
if (modCount != mc)
throw new ConcurrentModificationException();
}
}
}
abstract class HashIterator {
Node<K,V> next; // next entry to return
Node<K,V> current; // current entry
int expectedModCount; // for fast-fail
int index; // current slot
HashIterator() {
expectedModCount = modCount;
Node<K,V>[] t = table;
current = next = null;
index = 0;
if (t != null && size > 0) { // advance to first entry
do {} while (index < t.length && (next = t[index++]) == null);
}
}
public final boolean hasNext() {
return next != null;
}
//周遊順序是按照從上到下,從左到右的的順序來周遊
final Node<K,V> nextNode() {
Node<K,V>[] t;
Node<K,V> e = next;
//因為hashmap也是線程不安全的,是以要儲存modCount。用于fail-fast政策
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (e == null)
throw new NoSuchElementException();
if ((next = (current = e).next) == null && (t = table) != null) {
do {} while (index < t.length && (next = t[index++]) == null);
}
return e;
}
public final void remove() {
Node<K,V> p = current;
if (p == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
current = null;
K key = p.key;
removeNode(hash(key), key, null, false, false);
expectedModCount = modCount;
}
}
Fail-Fast 機制
我們知道 java.util.HashMap 不是線程安全的,是以如果在使用疊代器的過程中有其他線程修改了map,那麼将抛出ConcurrentModificationException,這就是所謂fail-fast政策。這一政策在源碼中的實作是通過 modCount 域,modCount 顧名思義就是修改次數,對HashMap 内容的修改都将增加這個值,那麼在疊代器初始化過程中會将這個值賦給疊代器的 expectedModCount。在疊代過程中,判斷 modCount 跟 expectedModCount 是否相等,如果不相等就表示已經有其他線程修改了 Map:注意到 modCount 聲明為 volatile,保證線程之間修改的可見性。
後續将會持續更新紅黑樹等。。。