本文介绍了可以存储在 HashMap 中的键(对象)数量的理论限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于可以存储在 HashMap 中的键条目的数量是否有理论上的限制,或者最大值是否完全取决于可用的堆内存?

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?

另外,哪种数据结构最适合存储大量对象(比如几十万个对象)?

Also, which data structure is the best to store a very large number of objects (say several hundred thousand objects)?

推荐答案

查看该类的文档,我会说理论上的限制是 Integer.MAX_VALUE (2-1 = 2147483647) 个元素.

Looking at the documentation of that class, I would say that the theoretical limit is Integer.MAX_VALUE (2-1 = 2147483647) elements.

这是因为要正确实现这个类,size() 方法必须返回一个 int 表示键/值对的数量.

This is because to properly implement this class, the size() method is obliged to return an int representing the number of key/value pairs.

来自 的文档HashMap.size()

返回:此映射中键值映射的数量

注意:这个问题与一个列表最多可以容纳多少数据非常相似.

Note: This question is very similar to How many data a list can hold at the maximum.

哪种数据结构最适合存储大量对象(比如几十万个对象)?

我想说这取决于您需要存储什么以及您需要什么类型的访问权限.所有内置集合可能都针对大批量进行了优化.

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.

这篇关于可以存储在 HashMap 中的键(对象)数量的理论限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 11:28