Java 可以存储在 HashMap 中的键(对象)数量的理论限制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4123743/
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
Theoretical limit for number of keys (objects) that can be stored in a HashMap?
提问by Ebbu Abraham
Is there a theoretical limit for the number of key entries that can be stored in a HashMap or does the maximum purely depend on the heap memory available?
可以存储在 HashMap 中的键条目数量是否有理论上的限制,或者最大值完全取决于可用的堆内存?
Also, which data structure is the best to store a very large number of objects (say several hundred thousand objects)?
另外,哪种数据结构最适合存储大量对象(比如几十万个对象)?
采纳答案by aioobe
Is there a theoretical limit for the number of key entries that can be stored in a HashMap or does it purely depend on the heapmemory available ?
可以存储在 HashMap 中的键条目的数量是否有理论上的限制,还是纯粹取决于可用的堆内存?
Looking at the documentation of that class, I would say that the theoretical limit is Integer.MAX_VALUE
(231-1 = 2147483647) elements.
查看该类的文档,我会说理论限制是Integer.MAX_VALUE
(2 31-1 = 2147483647) 个元素。
This is because to properly implement this class, the size()
method is obliged to return an int
representing the number of key/value pairs.
这是因为要正确实现此类,该size()
方法必须返回int
表示键/值对数量的 。
From the documentation of HashMap.size()
从文档 HashMap.size()
Returns:the number of key-value mappings in this map
返回:此映射中的键值映射数
Note: This question is very similar to How many data a list can hold at the maximum.
注意:这个问题非常类似于列表最多可以容纳多少数据。
which data structure is the best to store a very large number of objects(say several hundred thousand objects)?
哪种数据结构最适合存储大量对象(比如几十万个对象)?
I would say it depends on what you need to store and what type of access you require. All built in collections are probably well optimized for large quantities.
我会说这取决于您需要存储什么以及您需要什么类型的访问权限。所有内置集合可能都针对大量进行了优化。
回答by Bozho
HashMap
holds the values in an array, which can hold up to Integer.MAX_VALUE
. But this does not count collisions. Each Entry
has a next
field, which is also an entry. This is how collisions (two or more objects with the same hashcode) are resolved. So I wouldn't say there is any limit (apart from the available memory)
HashMap
将值保存在一个数组中,最多可容纳Integer.MAX_VALUE
. 但这不算碰撞。每个Entry
都有一个next
字段,它也是一个条目。这就是冲突(具有相同哈希码的两个或多个对象)的解决方法。所以我不会说有任何限制(除了可用内存)
Note that if you exceed Integer.MAX_VALUE
, you'll get unexpected behaviour from some methods, like size()
, but get()
and put()
will still work. And they will work, because the hashCode()
of any object will return an int
, hence by definition each object will fit in the map. And then each object will collide with an existing one.
请注意,如果超过Integer.MAX_VALUE
,您会从某些方法中获得意外行为,例如size()
, butget()
并且put()
仍然有效。它们会起作用,因为hashCode()
任何对象的 都会返回int
,因此根据定义,每个对象都适合映射。然后每个对象将与现有对象发生碰撞。
回答by Martijn Verburg
I agree with @Bozho's and will also add that you should read the Javadocon HashMap carefully. Note how it discusses the initial capacity and load factor and how they'll affect the performance of the HashMap.
我同意@Bozho 的观点,并补充说您应该仔细阅读HashMap 上的Javadoc。请注意它如何讨论初始容量和负载因子以及它们将如何影响 HashMap 的性能。
HashMap is perfectly fine for holding large sets of data (as long as you don't run out of keys or memory) but performance can be an issue.
HashMap 非常适合保存大量数据(只要您没有用完键或内存),但性能可能是一个问题。
You may need to look in to distributed caches/data grids if you find you can't manipulate the datasets you need in a single Java/JVM program.
如果您发现无法在单个 Java/JVM 程序中操作所需的数据集,则可能需要查看分布式缓存/数据网格。
回答by pgras
There is no theoretical limit, but there is a limit of buckets to store different entry chains (stored under a different hashkey). Once you reach this limit every new addition will result in a hash collision -- but this is no a problem except for performance...
没有理论上的限制,但是存储不同条目链(存储在不同的哈希键下)的存储桶是有限制的。一旦达到这个限制,每一个新的添加都会导致哈希冲突——但这除了性能之外没有问题......