Java | JDK8下的ConcurrentHashMap#get
1 public V get(Object key) { 2 Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek; 3 //寻址 4 int h = spread(key.hashCode()); 5 if ((tab = table) != null && (n = tab.length) > 0 && 6 (e = tabAt(tab, (n - 1) & h)) != null) { 7 //当头节点的hash值与key的hash值相同时,判断key的内容知否相同 8 if ((eh = e.hash) == h) { 9 if ((ek = e.key) == key || (ek != null && key.equals(ek))) 10 return e.val; 11 } 12 //如果头节点的hash值eh<0 红黑树存储 直接寻找 13 else if (eh < 0) 14 return (p = e.find(h, key)) != null ? p.val : null; 15 //链表查找 16 while ((e = e.next) != null) { 17 if (e.hash == h && 18 ((ek = e.key) == key || (ek != null && key.equals(ek)))) 19 return e.val; 20 } 21 } 22 return null; 23 }