版本:jdk 1.8.0_161
工具:IntelliJ IDEA 2018.1
图形: 鉴于HashMap 里面的图片显示不了,不再贴图
参考:这个里面的讲的很好啊,不写了,直接看这个吧https://blog.csdn.net/u010723709/article/details/48007881
Node :核心内部类: 内部使用 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
树节点类,另外一个核心的数据结构。当链表长度过长的时候,会转换为TreeNode。但是与HashMap不相同的是,它并不是直接转换为红黑树,而是把这些结点包装成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 */
// 核心思想 : :
// 根据hash值计算节点插入在tab的位置,
// 如果该位置为null,则直接插入,否则插入到链表或者树中
final V putVal(K key, V value, boolean onlyIfAbsent) {
// 如果插入的 key value 为null 那么,抛异常,终止执行
if (key == null || value == null) throw new NullPointerException();
int hash = spread(key.hashCode());
int binCount = 0;
// 遍历table,进行节点插入操作
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();
// 如果数组 节点的数据为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()方法为协助扩容方法,当调用该方法的时候,
// nextTable一定已经创建了,所以该方法主要则是进行复制工作
else if ((fh = f.hash) == MOVED)
tab = helpTransfer(tab, f);
else {
V oldVal = null;
synchronized (f) {
if (tabAt(tab, i) == f) {
// 如果f.hash >= 0 表示是链表结构,则遍历链表,
// 如果存在当前key节点则替换value,否则插入到链表尾部。
// 如果f是TreeBin类型节点,则按照红黑树的方法更新或者增加节点
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(默认是8),则将链表转换为红黑树结构
if (binCount >= TREEIFY_THRESHOLD)
treeifyBin(tab, i);
if (oldVal != null)
return oldVal;
break;
}
}
}
// 调用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);
}
查
根据hash值获取table中的Node节点(tabAt(tab, (n - 1) & h)),然后根据链表或者树形方式找到相对应的节点,返回其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
// 根据hash值获取table中的Node节点(tabAt(tab, (n - 1) & h))
if ((tab = table) != null && (n = tab.length) > 0 &&
(e = tabAt(tab, (n - 1) & h)) != null) {
// 头节点,匹配则返回
if ((eh = e.hash) == h) {
if ((ek = e.key) == key || (ek != null && key.equals(ek)))
return e.val;
}
// tree 方式找到相对应的节点,返回其value值。
else if (eh < 0)
return (p = e.find(h, key)) != null ? p.val : null;
// linkedList 方式找到相对应的节点,返回其value值。
while ((e = e.next) != null) {
if (e.hash == h &&
((ek = e.key) == key || (ek != null && key.equals(ek))))
return e.val;
}
}
return null;
}
删