可以将多少个元素存储在Java中的HashMap对象中 [英] How many elements can I store in a HashMap object in Java
问题描述
我知道这取决于系统中可用的内存,也取决于一个好的散列函数,但总的来说,我想知道您使用的最大的地图是什么,如果它运行良好或者需要进行任何调整以使其正常工作。
A HashMap $ c $ Java中的c>最多可以有2 ^ 30个存储区用于存储条目 - 这是因为
java.util.HashMap
使用的存储区分配技术需要存储区的数量为2的幂,并且由于int使用Java签名,因此最大正值为2 ^ 31 - 1,所以2的最大幂为2 ^ 30。
<但是,实际上对于可以存储在HashMap中的键/值对有多少没有编程限制 - 一旦你使用了 size()
函数就会停止准确通过2 ^ 31 - 1。这是因为处理碰撞的方式 - 连接在同一个桶中的键/值对,如节点在一个 LinkedList
。
一般来说,如果你在任何地方接近2 ^ 30的东西,需要跟踪真实世界的应用程序,您需要的RAM比您在一台机器上可以依赖的要多得多。我曾经使用过的最大的HashMap坐在一个JVM中,有几千万条记录,都非常轻量级。
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.
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.
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
.
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对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!