HashMap get 单链表查询

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);
            }
        }

总结:

get时 先hash 然后key   

e.next == null 代表到达最后一个元素节点   则结束

猜你喜欢

转载自blog.csdn.net/qq_29857681/article/details/80740098