1. 概述
HashMap 基於哈希表實現,通過 key 查找 對應的 value ,時間複雜度爲 O(1),即常數階;
HashMap 的底層數據結構爲 數組 + 鏈表/紅黑樹;
數組的長度爲 n,下圖中的數組長度爲4,n 爲4, 鍵值對放入那個桶的計算方法爲 (n - 1) & key.hash。
2. 靜態常量
(1)數組的默認容量:16,必須是2的n次冪;容量指的是數組的長度
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
默認容量爲什麼是2的n次冪?
key在數組中的下標爲: (n - 1) & hash,hash爲key的hash值。如果這個n是2的N次冪,那麼hash相當於和111***111做與運算,數據分散的就比較均勻。如果n不是2的N次冪,如果 n 爲 1111,hash和1110做與運算,那麼最後一位怎麼與都是0,那相當hash 的最右位沒用了。
(2)最大的容量爲 2^30
static final int MAXIMUM_CAPACITY = 1 << 30;
(3) 負載因子, 默認負載因子爲0.75
static final float DEFAULT_LOAD_FACTOR = 0.75f;
(3)一個桶中從鏈表轉成紅黑樹的閾值爲8
static final int TREEIFY_THRESHOLD = 8;
(4)一個桶中,由紅黑樹轉成鏈表的閾值,resize的時候可能會用到這個值
static final int UNTREEIFY_THRESHOLD = 6;
(5) 樹形化最小hash鍵值對總數,如果桶內元素已經達到轉化紅黑樹閾值,但是鍵值對未達到MIN_TREEIFY_CAPACITY,則值進行擴容resize(),不進行樹形化。
static final int MIN_TREEIFY_CAPACITY = 64;
3. 節點
節點用來 存儲 鍵值對,指向下一個節點的指針,當前節點的 hash 值。
// 鍵值對
static class Node<K,V> implements Map.Entry<K,V> {
// Hash 值
final int hash;
// key 鍵值
final K key;
// vlaue 值
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;
}
// get 函數
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
// hashcode
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
// 設置 新值,返回舊值
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;
if (o instanceof Map.Entry) {
Map.Entry<?,?> e = (Map.Entry<?,?>)o;
if (Objects.equals(key, e.getKey()) &&
Objects.equals(value, e.getValue()))
return true;
}
return false;
}
}
4. 成員變量和方法
3.1 求key 的 hash值
>>> 無符號右移,忽略符號位,空位都以0補齊。 hashcode 爲 int, 32位。
static final int hash(Object key) {
int h;
// 如果 hash == null, hash 值爲0
// 否則 哈希值 = key 的 hashcode &
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
可以看到它並沒有直接使用Object中生成hashcode的方法,而是 (h = key.hashCode()) ^ (h >>> 16) ,這個方法叫擾動函數,和之前的要將capacity設計成2^n一樣,也是爲了減少碰撞用的。
根據前面可知,key在Node[]中的下標爲 key.hashCode & (n-1),我們知道key.hashCode是一個很長的int類型的數字(範圍大概40億),而n-1顯然沒有這麼長,如果直相與,那麼只有key.hashCode的後面幾位參與運算了,顯然會使得碰撞很激烈!加了這個函數之後,讓高位也想辦法參與到運算中來,這樣就有可能進一步降低碰撞的可能性了!
3.2 返回給定目標容量的兩倍冪
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;
}
假設cap的二進制爲01xx…xx。
對cap右移1位:01xx…xx,位或:011xx…xx,使得與最高位的1緊鄰的右邊一位爲1,
對cap右移2位:00011x..xx,位或:01111x..xx,使得從最高位的1開始的四位也爲1,
以此類推,int爲32位,所以在右移16位後異或最多得到32個連續的1,保證從最高位的1到末尾全部爲1。
最後讓結果+1,就得到了最近的大於cap的2的整數次冪。
3.3 數組
數組分配時,長度總是2的冪。(在某些操作中,我們還允許長度爲零,以允許當前不需要的引導機制。)
transient Node<K,V>[] table;
數組爲什麼是transient?
爲了解答這個問題,我們需要明確下面事實:
Object.hashCode方法對於一個類的兩個實例返回的是不同的哈希值
可以試想下面的場景:
我們在機器A上算出對象A的哈希值與索引,然後把它插入到HashMap中,然後把該HashMap序列化後,在機器B上重新算對象的哈希值與索引,這與機器A上算出的是不一樣的,所以我們在機器B上get對象A時,會得到錯誤的結果。
所以說,當序列化一個HashMap對象時,保存Entry的table是不需要序列化進來的,因爲它在另一臺機器上是錯誤的,所以屬性這裏爲transient。
因爲這個原因,HashMap重寫了writeObject與readObject 方法。
3.4 保存緩存entrySet ()
注意AbstractMap字段用於keySet()和values()。保存K/V對的Set。
transient Set<Map.Entry<K,V>> entrySet;
3.5 鍵值對的數量
transient int size;
3.6 在結構上修改這個HashMap的次數
結構修改是指改變HashMap中映射的數量或以其他方式修改其內部結構(例如,rehash)。此字段用於使HashMap集合視圖上的迭代器快速失效。(見ConcurrentModificationException)。
transient int modCount;
3.7 要調整大小的下一個大小值(容量*負載因子)
Threadhold表示當容量達到該值時,會進行resize 擴容。
int threshold;
3.8 哈希表的加載因子,即size > capacity * loadFactor,hashmap就要進行擴容。
final float loadFactor;
3.9 構造函數
構造參數,初始容量和加載因子。加載因子默認爲 0.75;
public HashMap(int initialCapacity, float loadFactor) {
// 初始容量 需要大於等於 0
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
// 初始容量 需要 <= 最大容量
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
// loadFactor 必須是浮點數,且大於 0
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity);
}
HashMap 初始化的初始化容量。
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
默認構造參數。
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
使用 Map m 構建一個 hashMap。
public HashMap(Map<? extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
putMapEntries(m, false);
}
將 m 的值賦給 table;
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
// m 的數量
int s = m.size();
if (s > 0) {
// table 爲空,則未初始化
if (table == null) { // pre-size
// 容量
float ft = ((float)s / loadFactor) + 1.0F;
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
// t 大於 閾值,則初始化閾值
if (t > threshold)
threshold = tableSizeFor(t);
}
// 已初始化,鍵值對的數量大於閾值,擴容
else if (s > threshold)
resize();
// 將 m 中的元素放到 table 中
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);
}
}
}
3.10 鍵值對數量 和 是否爲空
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
5. get 方法
get 方法 根據 key 查找節點 。
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
根據 key 的 hash 值 和 key 查找節點。
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab;
Node<K,V> first, e;
int n; K k;
// table 不爲空,長度大於0
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) {
// 紅黑樹調用 getTreeNode()
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;
}
3.12 查看是否包含一個 key
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
}
3.13 是否包含一個值,
public boolean containsValue(Object value) {
Node<K,V>[] tab; V v;
if ((tab = table) != null && size > 0) {
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next) {
if ((v = e.value) == value ||
(value != null && value.equals(v)))
return true;
}
}
}
return false;
}
6. put 方法
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 爲 null 或 爲 0
// resize 擴容
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// 對 Hash 碼 進行取模運算
// i = (n - 1) & hash ,取得值應該放入的桶
// 如果 桶 i 爲 null 則新建桶
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
// 桶已經被初始化
Node<K,V> e;
K k;
// 如果 當前節點的 hash 值等於 插入的 key地址相同,hash 值也相同
// 或者 當前節點的 key 與 插入的 key相同,
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;
// 如果size 大於 threshold,擴容
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
樹化 hash & n-1 的那個桶。
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
// table 爲空 或 數組的長度 小於 MIN_TREEIFY_CAPACITY
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);
}
}
將指定映射的所有映射覆制到此映射。這些映射將替換當前指定映射中任意鍵的映射。
public void putAll(Map<? extends K, ? extends V> m) {
putMapEntries(m, true);
}
7. resize 擴容操作
resize擴容操作主要用在兩處:
向一個空的HashMap中執行put操作時,會調用resize()進行初始化,要麼默認初始化,capacity爲16,要麼根據傳入的值進行初始化。
put操作後,檢查到size已經超過threshold,那麼便會執行resize,進行擴容,如果此時capcity已經大於了最大值,那麼便把threshold置爲int最大值,否則,對capcity,threshold進行擴容操作。
發生了擴容操作,那麼必須Map中的所有的數進行再散列,重新裝入。
擴容一次,容量擴充兩倍。
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,已經初始化
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;
// 一個新的 table
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
// 處理舊的 table
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
// 獲取舊錶 桶j 的頭結點
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;
}
8. 移除節點
移除一個節點:
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
移除節點:
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;
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;
}
清除所有鍵值對。
public void clear() {
Node<K,V>[] tab;
modCount++;
if ((tab = table) != null && size > 0) {
size = 0;
for (int i = 0; i < tab.length; ++i)
tab[i] = null;
}
}