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) { if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) return first; 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; }
外层第一个if语句用于判断哈希表是否为空、哈希表的长度是否合法,同时通过计算出来的哈希值确定桶索引。(数组的哪一行)
内层第一个if语句用于判断桶中首个元素是否满足查找需求(hash值、key值),若满足返回。
若不满足进入内层第二个if语句,判断桶中首元素是否是树节点,若满足调用树节点的方法在红黑树中查找对应的元素,找到并返回;若不满足则挨个找。