Why ArrayList grows at a rate of 1.5, but for Hashmap it's 2?

后端 未结 7 1127
暖寄归人
暖寄归人 2020-12-28 17:28

As per Sun Java Implementation, during expansion, ArrayList grows to 3/2 it\'s initial capacity whereas for HashMap the expansion rate is double. What is reason behind this?

相关标签:
7条回答
  • 2020-12-28 18:13

    Hashing takes advantage of distributing data evenly into buckets. The algorithm tries to prevent multiple entries in the buckets ("hash collisions"), as they will decrease performance.

    Now when the capacity of a HashMap is reached, size is extended and existing data is re-distributed with the new buckets. If the size-increas would be too small, this re-allocation of space and re-dsitribution would happen too often.

    0 讨论(0)
提交回复
热议问题