Hashmap散列表大小限制小于数组索引的最大允许限制 [英] Hashmap hashtable size limit less than max allowed limit for array index

查看:547
本文介绍了Hashmap散列表大小限制小于数组索引的最大允许限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Java中,常规数组的索引可以达到的最大值, int 类型 2提升到31减去-1 ,因为 HashMap MAXIMUM_CAPACITY 也是一个 int ,它也可以上升到那个值。



但由于 HashMap 在内部需要表长度(桶大小)为两个 so限制被缩减为 - static final int MAXIMUM_CAPACITY = 1< 30; ,因为该值为最接近的两个幂 1 <31 -1



我的理解是否正确?



所有答案此处仅提及符号位限制,但不提供 power of two

  / ** 
*表格,必要时调整大小。长度必须始终为二的幂。
* /

transient Entry< K,V> [] table =(Entry< K,V> [])EMPTY_TABLE;

另外,我理解数组的大小限制 Hashmap (桶大小)与系统/对象/堆内存限制无关,但max_range < c $ c> int 仅数据类型(索引数据类型)和其他逻辑要求(如二的幂等)。

解决方案

数组的限制为2 ^^ 30,因为这是两个数组中最大的数, 。然而,没有理由建议散列映射局限于这个大小,而是在这一点上,散列映射降级到链表的散列(或Java 8中的树),即没有限制每个存储桶中的条目数。


I just wish to validate my below understanding so please suggest.

In Java, regular array can have indices up to the maximum value of int type which is 2 raised to power 31 minus -1 and since HashMap MAXIMUM_CAPACITY is an int too, it can go up to that value too.

But since HashMap internally needs table length(bucket size) to be a power of two so limit gets curtailed to - static final int MAXIMUM_CAPACITY = 1 << 30; since that value is nearest power of two to 1<<31 -1 .

Am I correct in my understanding?

All answers here mention only about sign bit limit but not power of two requirement,

/**
     * The table, resized as necessary. Length MUST Always be a power of two.
     */

transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;

Also, I understand that size limit for array or Hashmap (bucket size) has nothing to do with system / object / heap memory limitations but max_range for int data type only (index data type)and other logical requirements (like power of two etc).

解决方案

The limit for the array is 2^^30 as this is the largest power of two you can have a for an array. However, there is no reason to suggest that the hash map is limited to this size, but rather it is around this point that the hash map degrades in to a hash of linked lists (or tree in Java 8) i.e there is no limit to the number of entries in each bucket.

这篇关于Hashmap散列表大小限制小于数组索引的最大允许限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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