版本:jdk 1.8.0_161
工具:IntelliJ IDEA 2018.1
圖形: 鑒于HashMap 里面的圖片顯示不了,不再貼圖
參考:這個里面的講的很好啊,不寫了,直接看這個吧https://blog.csdn.net/u010723709/article/details/48007881
Node :核心內(nèi)部類: 內(nèi)部使用 final 和 volatile 修飾
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
volatile V val;
volatile Node<K,V> next;
Node(int hash, K key, V val, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.val = val;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return val; }
public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
public final String toString(){ return key + "=" + val; }
public final V setValue(V value) {
throw new UnsupportedOperationException();
}
public final boolean equals(Object o) {
Object k, v, u; Map.Entry<?,?> e;
return ((o instanceof Map.Entry) &&
(k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
(v = e.getValue()) != null &&
(k == key || k.equals(key)) &&
(v == (u = val) || v.equals(u)));
}
Node<K,V> find(int h, Object k) {
Node<K,V> e = this;
if (k != null) {
do {
K ek;
if (e.hash == h &&
((ek = e.key) == k || (ek != null && k.equals(ek))))
return e;
} while ((e = e.next) != null);
}
return null;
}
}
TreeNode
樹節(jié)點類,另外一個核心的數(shù)據(jù)結(jié)構(gòu)。當(dāng)鏈表長度過長的時候,會轉(zhuǎn)換為TreeNode。但是與HashMap不相同的是,它并不是直接轉(zhuǎn)換為紅黑樹,而是把這些結(jié)點包裝成TreeNode放在TreeBin對象中,由TreeBin完成對紅黑樹的包裝。而且TreeNode在ConcurrentHashMap集成自Node類,而并非HashMap中的集成自LinkedHashMap.Entry<K,V>類,也就是說TreeNode帶有next指針,這樣做的目的是方便基于TreeBin的訪問。
原文:https://blog.csdn.net/u010723709/article/details/48007881
增
/*
* Encodings for Node hash fields. See above for explanation.
*/
static final int MOVED = -1; // hash for forwarding nodes
static final int TREEBIN = -2; // hash for roots of trees
static final int RESERVED = -3; // hash for transient reservations
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
public V put(K key, V value) {
return putVal(key, value, false);
}
/** Implementation for put and putIfAbsent */
// 核心思想 : :
// 根據(jù)hash值計算節(jié)點插入在tab的位置,
// 如果該位置為null,則直接插入,否則插入到鏈表或者樹中
final V putVal(K key, V value, boolean onlyIfAbsent) {
// 如果插入的 key value 為null 那么,拋異常,終止執(zhí)行
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode());
int binCount = 0;
// 遍歷table,進行節(jié)點插入操作
for (Node<K,V>[] tab = table;;) {
Node<K,V> f; int n, i, fh;
// tab為空,則進行初始化操作:initTable()
if (tab == null || (n = tab.length) == 0)
tab = initTable();
// 如果數(shù)組 節(jié)點的數(shù)據(jù)為null ,那么無鎖的操作
else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
if (casTabAt(tab, i, null,
new Node<K,V>(hash, key, value, null)))
break; // no lock when adding to empty bin
}
// 如果檢測到 Moved 那么其他線程正在擴容,幫助線程擴容
// helpTransfer()方法為協(xié)助擴容方法,當(dāng)調(diào)用該方法的時候,
// nextTable一定已經(jīng)創(chuàng)建了,所以該方法主要則是進行復(fù)制工作
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
synchronized (f) {
if (tabAt(tab, i) == f) {
// 如果f.hash >= 0 表示是鏈表結(jié)構(gòu),則遍歷鏈表,
// 如果存在當(dāng)前key節(jié)點則替換value,否則插入到鏈表尾部。
// 如果f是TreeBin類型節(jié)點,則按照紅黑樹的方法更新或者增加節(jié)點
if (fh >= 0) {
binCount = 1;
for (Node<K,V> e = f;; ++binCount) {
K ek;
if (e.hash == hash &&
((ek = e.key) == key ||
(ek != null && key.equals(ek)))) {
oldVal = e.val;
if (!onlyIfAbsent)
e.val = value;
break;
}
Node<K,V> pred = e;
if ((e = e.next) == null) {
pred.next = new Node<K,V>(hash, key,
value, null);
break;
}
}
}
else if (f instanceof TreeBin) {
Node<K,V> p;
binCount = 2;
if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
value)) != null) {
oldVal = p.val;
if (!onlyIfAbsent)
p.val = value;
}
}
}
}
if (binCount != 0) {
// 若鏈表長度 > TREEIFY_THRESHOLD(默認(rèn)是8),則將鏈表轉(zhuǎn)換為紅黑樹結(jié)構(gòu)
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
// 調(diào)用addCount方法,ConcurrentHashMap的size + 1
addCount(1L, binCount);
return null;
}
// 無鎖的操作 unsafe
static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
Node<K,V> c, Node<K,V> v) {
return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
}
查
根據(jù)hash值獲取table中的Node節(jié)點(tabAt(tab, (n - 1) & h)),然后根據(jù)鏈表或者樹形方式找到相對應(yīng)的節(jié)點,返回其value值。
public V get(Object key) {
Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
int h = spread(key.hashCode()); // 計算hash值
// 判斷table是否為空,如果為空,直接返回null
// 根據(jù)hash值獲取table中的Node節(jié)點(tabAt(tab, (n - 1) & h))
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {
// 頭節(jié)點,匹配則返回
if ((eh = e.hash) == h) {
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
// tree 方式找到相對應(yīng)的節(jié)點,返回其value值。
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
// linkedList 方式找到相對應(yīng)的節(jié)點,返回其value值。
while ((e = e.next) != null) {
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}
刪