Java hashmap 最大大小为 5770?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25609840/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:46:22  来源:igfitidea点击:

Java hashmap max size of 5770?

javahashmap

提问by SirNickParks

I was testing some file/profile transferring things today. I used a HashMapto store a players name and the value of there profile. However I noticed my hashmap only goes up to 5770 in size. Why is this and how can I fix it?

我今天正在测试一些文件/配置文件传输的东西。我使用 aHashMap来存储玩家姓名和那里的个人资料值。但是我注意到我的哈希图的大小只增加到 5770。为什么会这样,我该如何解决?

HashMap<String, String> temp = new HashMap<String, String>();
for(String s : dataFile.getConfigurationSection("users").getKeys(false)) {
        temp.put(s, dataFile.getString("users." + s + ".group"));
}

That's what i'm using to grab the player and their "group".

这就是我用来抓住玩家和他们的“组”的东西。

采纳答案by DanielM

HashMap is not limited, your problem is probably you have repeating keys..

HashMap 不受限制,您的问题可能是您有重复的键..

I would check if the key is contained already before putting it in the map:

在将其放入地图之前,我会检查密钥是否已包含:

if(temp.containsKey(s)){
 System.out.println("Erasing key "+s+" val "+temp.get(s));
}
temp.put(s, dataFile.getString("users." + s + ".group"));

回答by Ankur Singhal

HashMap is not limited, provided to have a load factor is increased.

HashMap 没有限制,只要增加一个负载因子即可。

In Sun's JVM, HashMapuses an array which is a power of 2. The largest power of two allowed for an array size is 2^30. And the largest number of elements you can have before the HashMap will try to double its size to 2^31 (which it cannot do) is (2^30 * loadFactor) or about 700 million for the default load factor.

在 Sun 的 JVM 中,HashMap使用的数组是 2 的幂。数组大小所允许的最大 2 的幂是2^30。在 HashMap 尝试将其大小加倍到 2^31(它不能这样做)之前,您可以拥有的最大元素数量是 ( 2^30 * loadFactor) 或默认加载因子约为 7 亿。