为什么 Java HashMap 的最大容量是 1<<30 而不是 1<<31?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21638080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is the maximum capacity of a Java HashMap 1<<30 and not 1<<31?
提问by tesnik03
Why is the maximum capacity of a Java HashMap 1<<30 and not 1<<31, even though the max value of an int is 231-1? The maximum capacity is initialized as static final int MAXIMUM_CAPACITY = 1 << 30;
为什么 Java HashMap 的最大容量是 1<<30 而不是 1<<31,即使 int 的最大值是 2 31-1?最大容量初始化为static final int MAXIMUM_CAPACITY = 1 << 30;
采纳答案by initramfs
Java uses signed integers which means the first bit is used to store the sign of the number (positive/negative).
Java 使用有符号整数,这意味着第一位用于存储数字的符号(正/负)。
A four byte integer has 32 bits in which the numerical portion may only span 31 bits due to the signing bit. This limits the range of the number to 2^31 - 1(due to inclusion of 0) to - (2^31).
一个四字节整数有 32 位,其中由于签名位的原因,数字部分可能只跨越 31 位。这将数字的范围限制为2^31 - 1(由于包含 0)到- (2^31)。
回答by Alex Johnson
You are thinking of unsigned, with signed upper range is (2^31)-1
您正在考虑无符号,有符号上限是 (2^31)-1
回答by supercat
While it would be possible for a hash map to handle quantities of items between 2^30 and 2^31-1 without having to use larger integer types, writing code which works correctly even near the upper limits of a language's integer types is difficult. Further, in a language which treats integers as an abstract algebraic ring that "wraps" on overflow, rather than as numbers which should either yield numerically-correct results or throw exceptions when they cannot do so, it may be hard to ensure that there aren't any cases where overflows would cause invalid operations to go undetected.
虽然哈希映射可以处理 2^30 和 2^31-1 之间的项目数量而不必使用更大的整数类型,但编写即使接近语言整数类型上限也能正常工作的代码是很困难的。此外,在将整数视为“包装”溢出的抽象代数环的语言中,而不是将其视为应该产生数值正确的结果或在它们不能这样做时抛出异常的数字中,可能很难确保有'不是任何溢出会导致无效操作未被检测到的情况。
Specifying an upper limit of 2^30 or even 2^29, and ensuring correct behavior on things no larger than that, is often much easier than trying to ensure correct behavior all the way up to 2^31-1. Absent a particular reason to squeeze out every last bit of range, it's generally better to use the simpler approach.
指定 2^30 或什至 2^29 的上限,并确保不大于该值的事物的正确行为通常比尝试确保一直到 2^31-1 的正确行为容易得多。如果没有特别的理由来挤出范围的最后一点,通常最好使用更简单的方法。
回答by Chandra Sekhar
By default, the int
data type is a 32-bit signed two's complement integer, which has a minimum value of -2^31
and a maximum value of (2^31)-1
, ranges from –2,147,483,648 to 2,147,483,647.
The first bit is reserved for the sign bit — it is 1 if the number is negative and 0 if it is positive.
1 << 30
is equal to 1,073,741,824
it's two's complement binary integer is 01000000-00000000-00000000-00000000.
默认情况下,int
数据类型为32 位有符号二进制补码整数,其最小值为-2^31
,最大值为(2^31)-1
,范围从 –2,147,483,648 到 2,147,483,647。
第一位是为符号位保留的——如果数字为负,则为 1,如果为正,则为 0。
1 << 30
等于1,073,741,824
它的二进制补码二进制整数是 01000000-00000000-00000000-00000000。
1 << 31
is equal to -2,147,483,648.
it's two's complement binary integer is 10000000-00000000-00000000-00000000.
1 << 31
等于-2,147,483,648。
它的二进制补码二进制整数是 10000000-00000000-00000000-00000000-00000000-000000000。
It says the maximum size to which hash-map can expand is 1,073,741,824 = 2^30.
它说 hash-map 可以扩展到的最大大小是 1,073,741,824 = 2^30。