HashMap Load Factor and Initial Capacity

Initial Capacity and Load Factor in HashMap are two parameters that affect its performance.

Initial Capacity In HashMap

The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. Default initial capacity is 16.

Load factor In HashMap

The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets. Default load factor is 0.75.

It means that after storing the 12th (16 * 0.75 = 12) key into the HashMap , internal data structures are rebuilt and its capacity will become 32.

  • rakeshkamboj

    HashSet also follow same rule,only differece between both is HashMap contains entry(key and value).

  • kavya tg

    Load factor represents at what level HashMap should increase its own size. For example product of capacity and load factor as (default size) 16 * 0.75 = 12. This represents that after storing the 12th key – value pair into the HashMap , its capacity becomes 32 in Java HashMap

    • Hi Kavya,

      Thanks for visiting. I have already put this information. Is there any difference you want to specify here?