Search code examples
javaload-factor

Map load factor, how map grows


As per my understanding, and what I have read

The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased

So, when loadfactor is .8(80%), with map size of 10, Map will grow by size 10, when 8 elements are put in Map.

So, now Map has size 20. My doubt is when next 10 element space will be added to Map.

  • when Map is again 80% full, that is when 16 elements are put in Map.

or

  • When 18 elements are put in Map.

Solution

  • That will be at 16. If you look at the java code for HashMap:

    threshold = (int)(newCapacity * loadFactor);
    

    where new capacity is the new size. Therefore the limit in your example will be 16.