HashSet,Vector,LinkedList的最大大小 [英] Maximum size of HashSet, Vector, LinkedList
问题描述
HashSet
,向量
, LinkedList的最大大小是多少?
?我知道 ArrayList
可以存储超过3277000个数字。
What is the maximum size of HashSet
, Vector
, LinkedList
? I know that ArrayList
can store more than 3277000 numbers.
但是列表的大小取决于内存)尺寸。如果它达到最大值,JDK会抛出一个 OutOfMemoryError
。
However the size of list depends on the memory (heap) size. If it reaches maximum the JDK throws an OutOfMemoryError
.
但是我不知道数量的限制 HashSet
,中的元素
和 LinkedList
。
But I don't know the limit for the number of elements in HashSet
, Vector
and LinkedList
.
推荐答案
这些结构没有指定的最大大小。
There is no specified maximum size of these structures.
大小限制可能在 Integer.MAX_VALUE
(即2147483647,大约20亿个元素)范围内,因为这是Java中数组的最大大小。
The actual practical size limit is probably somewhere in the region of Integer.MAX_VALUE
(i.e. 2147483647, roughly 2 billion elements), as that's the maximum size of an array in Java.
- A
HashSet
在内部使用HashMap
,因此它具有与
- 相同的最大大小A
HashMap
使用一个总是具有2,因此它最多可以是2 30 = 1073741824个元素大(因为下一个2的幂大于Integer.MAX_VALUE
) - 通常元素数量最多为桶数乘以负载系数(默认为0.75)。 但,当
HashMap
停止调整大小时,它仍会 允许您添加元素, bucket通过链表进行管理。因此,HashMap
/HashSet
中元素的唯一限制是内存。
< ul - 相同的最大大小A
- A
向量
在内部使用数组,其最大大小完全Integer.MAX_VALUE
,因此它不能支持更多的元素
- A
LinkedList
/ em>使用数组作为底层存储,这样就不会限制大小。它使用没有固有限制的典型的双向链表结构,因此其大小仅由可用存储器限定。请注意,如果LinkedList
大于Integer.MAX_VALUE
,则会错误报告大小,因为它使用int
字段存储size()
的大小和返回类型是int
- A
HashSet
uses aHashMap
internally, so it has the same maximum size as that- A
HashMap
uses an array which always has a size that is a power of two, so it can be at most 230 = 1073741824 elements big (since the next power of two is bigger thanInteger.MAX_VALUE
). - Normally the number of elements is at most the number of buckets multiplied by the load factor (0.75 by default). However, when the
HashMap
stops resizing, then it will still allow you to add elements, exploiting the fact that each bucket is managed via a linked list. Therefore the only limit for elements in aHashMap
/HashSet
is memory.
请注意,虽然
集合
API 定义集合
与多于Integer.MAX_VALUE
元素应该行为。最重要的是,它声明此size()
文档:Note that while the
Collection
API does define how aCollection
with more thanInteger.MAX_VALUE
elements should behave. Most importantly it states this thesize()
documentation:
如果此集合包含超过
Integer.MAX_VALUE
元素,返回Integer.MAX_VALUE
。请注意,虽然
HashMap
,HashSet
和LinkedList
支持超过Integer.MAX_VALUE
元素,无 )方法(即它们只是让内部size
字段溢出)。Note that while
HashMap
,HashSet
andLinkedList
seem to support more thanInteger.MAX_VALUE
elements, none of those implement thesize()
method in this way (i.e. they simply let the internalsize
field overflow).这导致我相信其他操作也在此条件下没有明确定义。
This leads me to believe that other operations also aren't well-defined in this condition.
所以我会说它安全以使用具有最多
Integer.MAX_VLAUE
元素的通用集合。如果您知道您需要存储更多的内容,那么您应该切换到实际支持此功能的专用集合实现。So I'd say it's safe to use those general-purpose collections with up to
Integer.MAX_VLAUE
elements. If you know that you'll need to store more than that, then you should switch to dedicated collection implementations that actually support this.这篇关于HashSet,Vector,LinkedList的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- A