Java hashmap max size of 5770?

32,223

Solution 1

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

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"));

Solution 2

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

In Sun's JVM, HashMap uses 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.

Share:
32,223
SirNickParks
Author by

SirNickParks

Updated on July 09, 2022

Comments

  • SirNickParks
    SirNickParks almost 2 years

    I was testing some file/profile transferring things today. I used a HashMap to 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?

    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".

  • Cratylus
    Cratylus almost 10 years
    The arrays are indexed by integers and that is the "limitation" on maximum size of the array. The power of 2 is the pattern the array increases