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

后端 未结 7 1159
暖寄归人
暖寄归人 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:05

    The way HashMap is designed/implemented its underlying number of buckets must be a power of 2 (even if you give it a different size, it makes it a power of 2), thus it grows by a factor of two each time. An ArrayList can be any size and it can be more conservative in how it grows.

提交回复
热议问题