What can a hash map do if a key-value is added and the array is full?


#1

Question

In the context of this lesson, what can a hash map do if a key-value is added and the array is full?

Answer

When the hash map array is full, and a key-value is added, the hash map can resize the array to be larger to fit more key-values. Usually, a new array is created, instead of changing the original array. After creating the new array, it must then go through every key-value in the older array, and rehash them so that they correctly map to the larger array’s indexes. After this, the new key-value would be added.

For example, say that we had a hash map with an initial size of 10. If a key-value is added and it is full, then it will create a new array with size 20. Then, it will go through each of the existing key-values and rehash all of them to the larger array, and then add the new key-value.