Hash conflicts resolve conflicts with linked lists. Which element is returned?

one way to resolve hash conflicts is called chain address, which is to string together value of the same key with a linked list. So, when you use these same key values, you get the linked list, but there are multiple values in the linked list, which value should be returned to the user?

May.18,2022

key = > hash = > value

hash = > [{"k": key, "v": value}, {"k": key, "v": value}]

if you want to take a value, you must send key into it. Then just make a comparison.


of course, go to a key in a comparison chain and find the value corresponding to key


 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 && //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;
    }

Source code is returned through hash and equals comparison

Menu