为什么ArrayList的生长以1.5的速率,但对于哈希映射它的2? [英] Why ArrayList grows at a rate of 1.5, but for Hashmap it's 2?

查看:295
本文介绍了为什么ArrayList的生长以1.5的速率,但对于哈希映射它的2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于每个Sun Java实现,扩张期间,ArrayList中长到3/2它的初始容量而对于HashMap中的扩张速度加倍。这背后有什么原因呢?

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?

根据实施,对于HashMap的,容量应该永远是两种力量。这可能是对的HashMap的行为的原因。但是,在这种情况下,问题是,对于为什么HashMap的容量应该总是在两个电源?

As per the implementation, for HashMap, the capacity should always be in the power of two. That may be a reason for HashMap's behavior. But in that case the question is, for HashMap why the capacity should always be in power of two?

推荐答案

在增加一个ArrayList的容量昂贵的部分被复制支持数组内容的新的(大)之一。

The expensive part at increasing the capacity of an ArrayList is copying the content of the backing array a new (larger) one.

有关HashMap的,这是创建一个新的支持数组和的所有地图项新的数组中为止。并且,容量越高,低碰撞的危险。这是更昂贵的,并解释了,为什么膨胀系数较高。之所以对1.5 2.0?我认为这是最佳实践或很好的折衷。

For the HashMap, it is creating a new backing array and putting all map entries in the new array. And, the higher the capacity, the lower the risk of collisions. This is more expensive and explains, why the expansion factor is higher. The reason for 1.5 vs. 2.0? I consider this as "best practise" or "good tradeoff".

这篇关于为什么ArrayList的生长以1.5的速率,但对于哈希映射它的2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆