Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java hashmap max size of 5770?

Tags:

java

hashmap

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

like image 957
SirNickParks Avatar asked Sep 01 '14 16:09

SirNickParks


People also ask

What is the maximum capacity of HashMap?

it's two's complement binary integer is 10000000-00000000-00000000-00000000. It says the maximum size to which hash-map can expand is 1,073,741,824 = 2^30.

Why initial capacity of HashMap is 16?

Initial Capacity of HashMap The initial capacity of the HashMap is the number of buckets in the hash table. It creates when we create the object of HashMap class. The initial capacity of the HashMap is 24, i.e., 16. The capacity of the HashMap is doubled each time it reaches the threshold.

What is the space complexity of a HashMap?

For hashmap, with the increase of the number of entries, the hashmap's space will increase linearly. So space complexity is O(n) .

Can we define size of HashMap in Java?

HashMap size() Method in Java util. HashMap. size() method of HashMap class is used to get the size of the map which refers to the number of the key-value pair or mappings in the Map. Parameters: The method does not take any parameters.


2 Answers

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.

like image 103
Ankur Singhal Avatar answered Oct 22 '22 14:10

Ankur Singhal


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"));
like image 32
DanielM Avatar answered Oct 22 '22 14:10

DanielM