InterviewSolution
| 1. |
What is the purpose of the initial capacity and load factor parameters of a HashMap? What are their default values? How is the threshold plays the role to decide the size? |
|
Answer» Initial capacity in HashMap plays a vital ROLE in the allocation of memory as we know that HashMap works on the key-value pair, that means the HashMap needed a large AMOUNT of memory to maintain the key and value, to overcome these challenges the HashMap works on the principal to allocate the optimum size, whenever the HashMap get instantiated, that is CALLED Initial capacity, which is usually a 16. Now let’s focus on load factor, so there is a concept of threshold value, which is always a 0.75 size of the total capacity, the significance of this is whenever any new record gets inserted in the HashMap, the HashMap COMPUTES the allocated size of the HashMap, then it checks whether this size reaches the threshold value then HashMap increased the size of 75% from the CURRENT size to augmented the storage for next incoming record. |
|