How does the getter method work?


#1

Question

In the context of this exercise, how does the getter (retrieve) method work?

Answer

The getter, or retrieve(), method works by utilizing the same hash() and compressor() methods that were used for assigning a new key-value in the hash map.

When we are retrieving an entry, we essentially need to find where it was assigned in the first place. By using hash() and compressor(), we can obtain this index. And, because hash maps always return the same hash code index for any key every time whether we’re adding or retrieving a key, this index should contain our target entry.

Later in the lesson, these methods will apply open addressing to the getter and setter, but the result will be essentially the same.