問題描述
我知道這取決于系統中的可用內存,也取決于良好的哈希函數,但總的來說,我想知道您使用過的最大映射是什么,以及它是否運行良好盒子或需要任何調整以使其正常工作.
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模板網!