I want to increase a counter for a ConcurrentHashmap, but the key can be deleted at any time by another thread. So I check if the key still exists before increasing the counter.
I have the following code:
if(this.myMap.containsKey(key))
{
this.myMap.put(key, this.myMap.get(key) + 1);
}
myMap is created like this:
private Map<String, Integer> myMap = new ConcurrentHashMap<String, Integer>();
But from time to time I get a NullPointer Exception in the line where I increase the integer. This doesn't happen often (maybe 1 out of 10 Million runs).
Is there another way to increase the counter? Or Do I even need a HashMap?