![]() |
VOOZH | about |
The capacity of a HashMap simply doubles when it reaches a certain threshold. The threshold depends on the initial capacity and the load factor of the HashMap. Consider Capacity as the number of spaces, a HashMap currently has to store elements and Load factor is a measure of how full the map is allowed to get before its capacity is increased. Size can be understood by the number of elements a HashMap has at a certain point in time or the count of occupied buckets.
When the entries in the HashMap increase than the product of the current capacity and load factor, the HashMap is rehashed, and the capacity of the map is doubled.
Note: If the load factor is higher, it decreases space overhead but increases lookup cost. Creating a HashMap with sufficiently large storage will allow mapping to be stored much more efficiently with low hashing operations.
There is no official method to check for capacity of a HashMap. We can go through reflection. But it is not recommended. We can see the following code how after reaching threshold (>3 size) the capacity is doubled.
Below we can see the warnings output in the console.
👁 Checking the capacity of HashMap