You're right. If this Map can be changed in several threads, it is possible that the first call to chm.get(key) will return a non-zero value, and the second call will return null (due to the removal of the key from another thread), and thus chm.get(key).doSomething() will throw a NullPointerException .
You can make this code stream safe by using a local variable to save the result of chm.get(key) :
ConcurrentHashMap<Integer, Integer> chm = new ConcurrentHashMap<Integer, Integer>(); Integer value = chm.get(key); if(value != null) { value.doSomething();
BTW, even if this Map not ConcurentHashMap , and only one thread had access to it, I would still use a local variable, since it is more efficient than calling the get() method twice.
EDIT:
As noted below, this fix will not prevent doSomething() called multiple times for the same key / value by different threads. Regardless of whether this desired behavior is incomprehensible.
If you want to prevent the possibility of calling doSomething() multiple threads for the same key / value, you can use chm.remove(key) to remove the key and get the value in the same step.
However, there is a risk that doSomething() will not be executed for some key / value at all, because if the first call to doSomething() exception, the other call to doSomething() will not be another thread, since the key / value pair will no longer be in Map . On the other hand, if you remove a key / value pair from the Map only after doSomething() is successful, you guarantee that doSomething() is successful at least once for all the key / value pairs that have been re-mapped from Map .
source share