put核心流程:
get核心流程:
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) {
//获取首节点,hash碰撞概览小,通常链表第一个节点就是值,没必要去循环遍历,处于效率
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
//如果不止一个节点,就需要循环遍历,存在多个hash碰撞
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;
}