Does a HashMap collision cause a resize?

前端 未结 4 1974
别那么骄傲
别那么骄傲 2021-01-14 15:03

When there is a collision during a put in a HashMap is the map resized or is the entry added to a list in that particular bucket?

4条回答
  •  春和景丽
    2021-01-14 15:27

    The documentation of java.util.HashMap explains exactly when the map is resized:

    An instance of HashMap has two parameters that affect its performance: initial capacity and load factor.

    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.

    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.

    The default initial capacity is 16, the default load factor is 0.75. You can supply other values in the map's constructor.

提交回复
热议问题