【发布时间】:2016-05-07 01:20:36
【问题描述】:
我只是想验证我的以下理解,所以请提出建议。
在 Java 中,常规数组的索引可以达到 int 类型的最大值,即 2 raised to power 31 minus -1,并且由于 HashMap MAXIMUM_CAPACITY 也是 int,它也可以达到该值。
但由于 HashMap 内部需要表长度(存储桶大小)为 power of two 所以限制被缩减为 - static final int MAXIMUM_CAPACITY = 1 << 30; 因为该值是 nearest power of two 到 1<<31 -1 。
我的理解正确吗?
所有答案here 仅提及符号位限制,但未提及power of two 要求,
/**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
另外,我了解array 或Hashmap(存储桶大小)的大小限制与system / object / heap memory 限制无关,但max_range 仅适用于int 数据类型(索引数据类型)和其他逻辑要求(比如两个的幂等)。
【问题讨论】:
标签: java collections