educative.io

Why is head being updated if a key is already present in a HashTable?

In the section on “Insertion in the Table” of this lesson, the description says -

First of all, we will check if the key is already present in the table. If yes, it will insert the value into the head and point the head to next node.

This description is incorrect and does not make sense -

  1. What do you mean by “insert the value into the head”? The matching “HashEntry” element can be anywhere in this “bucket”, not necessarily at the “head” reference, and the chain must be traversed until you find the matching element, to update the value in.
  2. Why will you update the “head” to point to the “next” node? What does that have to do with updating the value of a key for a HashEntry element in the “bucket”?

See the relevant code snippet for insertion, where neither of the above two items is being done

        //Find head of the chain

        int b_Index = getIndex(key);

        HashEntry head = bucket.get(b_Index);

        //Checks if the key is already exists

        while(head != null)

        {

            if (head.key.equals(key))

            {

                head.value = value;

                return;  // exit the function here, where is the change to "head"?

            }

            head = head.next;

        }