HashSet、Vector、LinkedList 的最大大小 [英] Maximum size of HashSet, Vector, LinkedList

查看:16
本文介绍了HashSet、Vector、LinkedList 的最大大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HashSetVectorLinkedList 的最大大小是多少?我知道 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.

但我不知道HashSetVectorLinkedList 中元素数量的限制.

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.

  • HashSet 在内部使用了 HashMap,因此它的最大大小与那个相同
    • HashMap 使用的数组大小始终为 2 的幂,因此它最多可以有 230 = 1073741824 个元素(从下一个2 的幂大于 Integer.MAX_VALUE).
    • 通常元素的数量最多是桶的数量乘以负载因子(默认为 0.75).然而,当 HashMap 停止调整大小时,它会仍然允许您添加元素,利用每个桶通过链接管理的事实列表.因此,HashMap/HashSet 中元素的唯一限制是内存.
    • A HashSet uses a HashMap 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 than Integer.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 a HashMap/HashSet is memory.

      请注意,虽然 Collection API 确实定义了具有多个 Integer.MAX_VALUE 元素的 Collection 应该如何表现.最重要的是,它声明了这个 size() 文档:

      Note that while the Collection API does define how a Collection with more than Integer.MAX_VALUE elements should behave. Most importantly it states this the size() documentation:

      如果此集合包含多个 Integer.MAX_VALUE 元素,则返回 Integer.MAX_VALUE.

      If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

      请注意,虽然HashMapHashSetLinkedList 似乎支持比Integer.MAX_VALUE 元素,none 以这种方式实现 size() 方法(即它们只是让内部 size 字段溢出).

      Note that while HashMap, HashSet and LinkedList seem to support more than Integer.MAX_VALUE elements, none of those implement the size() method in this way (i.e. they simply let the internal size 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屋!

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