我知道这取决于系统中的可用内存,也取决于良好的哈希函数,但总的来说,我想知道您使用过的最大映射是什么,以及它是否运行良好盒子或需要任何调整以使其正常工作.
I know that is determined by the memory available in the system, and also depending on a good hash function, but in general I'd like to know what is the biggest map you have used, and if it worked well out of the box or needed any adjustment to make it work adequately.
Java 中的 HashMap
最多可以有 2^30 个桶来存储条目 - 这是因为使用了桶分配技术java.util.HashMap
要求bucket的个数是2的幂,由于Java中int是有符号的,所以最大正值是2^31 - 1,所以最大是2的幂是 2^30.
A HashMap
in Java can have a maximum of 2^30 buckets for storing entries - this is because the bucket-assignment technique used by java.util.HashMap
requires the number of buckets to be a power of 2, and since ints are signed in Java, the maximum positive value is 2^31 - 1, so the maximum power of 2 is 2^30.
然而,实际上没有编程限制可以在 HashMap 中存储多少键/值对 - 一旦通过 2^31,size()
函数将不再准确 -1. 这是因为处理冲突的方式 - 位于同一存储桶中的键/值对是链接的,就像 LinkedList
中的节点一样.
However, there is in fact no programmatic limit on how many key/value pairs you can store in a HashMap - the size()
function will just stop being accurate once you pass 2^31 - 1. This is because of the way collisions are handled - key/value pairs that land in the same bucket are linked, like nodes in a LinkedList
.
不过,一般来说,如果您在实际应用程序中需要跟踪 2^30 件事情,那么您需要的 RAM 比在一台机器上依赖的要多得多.我在单个 JVM 中使用过的最大的 HashMap 有几千万个条目,都非常轻量级
In general, though, if you're getting anywhere close to 2^30 things you need to keep track of in a real-world application, you need a lot more RAM than you can rely on in one machine. The largest HashMap I've ever worked with that sat in a single JVM had a few tens of millions of entries, all very lightweight
这篇关于我可以在 Java 中的 HashMap 对象中存储多少个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!