Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Java HashMap's size() be out of sync with its actual entries' size?

Tags:

java

hashmap

size

I have a Java HashMap called statusCountMap.
Calling size() results in 30.
But if I count the entries manually, it's 31
This is in one of my TestNG unit tests. These results below are from Eclipse's Display window (type code -> highlight -> hit Display Result of Evaluating Selected Text).

statusCountMap.size()
     (int) 30
statusCountMap.keySet().size()
     (int) 30
statusCountMap.values().size()
     (int) 30
statusCountMap
     (java.util.HashMap) {40534-INACTIVE=2, 40526-INACTIVE=1, 40528-INACTIVE=1, 40492-INACTIVE=3, 40492-TOTAL=4, 40513-TOTAL=6, 40532-DRAFT=4, 40524-TOTAL=7, 40526-DRAFT=2, 40528-ACTIVE=1, 40524-DRAFT=2, 40515-ACTIVE=1, 40513-DRAFT=4, 40534-DRAFT=1, 40514-TOTAL=3, 40529-DRAFT=4, 40515-TOTAL=3, 40492-ACTIVE=1, 40528-TOTAL=4, 40514-DRAFT=2, 40526-TOTAL=3, 40524-INACTIVE=2, 40515-DRAFT=2, 40514-ACTIVE=1, 40534-TOTAL=3, 40513-ACTIVE=2, 40528-DRAFT=2, 40532-TOTAL=4, 40524-ACTIVE=3, 40529-ACTIVE=1, 40529-TOTAL=5}
statusCountMap.entrySet().size()
     (int) 30

What gives ? Anyone has experienced this ?
I'm pretty sure statusCountMap is not being modified at this point.
There are 2 methods (lets call them methodA and methodB) that modify statusCountMap concurrently, by repeatedly calling incrementCountInMap.

private void incrementCountInMap(Map map, Long id, String qualifier) {
    String key = id + "-" + qualifier;
    if (map.get(key) == null) {
        map.put(key, 0);
    }
    synchronized (map) {
        map.put(key, map.get(key).intValue() + 1);
    }
}

methodD is where I'm getting the issue. methodD has a TestNG @dependsOnMethods = { "methodA", "methodB" } so when methodD is executing, statusCountMap is pretty much static already. I'm mentioning this because it might be a bug in TestNG.
I'm using Sun JDK 1.6.0_24. TestNG is testng-5.9-jdk15.jar

Hmmm ... after rereading my post, could it be because of concurrent execution of outside-of-synchronized-block map.get(key) == null & map.put(key,0) that's causing this issue ?

like image 967
trix Avatar asked Mar 12 '11 15:03

trix


People also ask

What happens when HashMap is full?

When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

How does HashMap resize in Java?

HashMap provides a constructor method which can take an initial capacity value and eventually this constructor method will call another constructor method with default load factor value of 0.75. Here threshold is the one being used to determine when the resizing should be triggered.

Does Java HashMap shrink?

HashMap does not shrink when data is removed. Even if all keys are removed from HashMap , the inner size of it's table does not change.

What does keySet return in Java?

keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys or we can create a new set and store the key elements in them. Parameters: The method does not take any parameter.


1 Answers

I believe you can achieve this if you modify a key after it is added to a HashMap.

However in your case it appears to be just a case of modifying the same map in two threads without proper synchronization. e.g. in thread A, map.put(key, 0), thread B map.put(key2, 0) can results in a size of 1 or 2. If you do the same with remove you can end up with a size larger than you should.

like image 76
Peter Lawrey Avatar answered Nov 09 '22 13:11

Peter Lawrey