Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ConcurrentHashMap and for each loop

Supposed I have the following ConcurrentHashMap:

ConcurrentHashMap<Integer,String> identificationDocuments = new ConcurrentHashMap<Integer,String>();
        
identificationDocuments.put(1, "Passport");
identificationDocuments.put(2, "Driver's Licence");

How would I safely iterate over the map with a for each loop and append the value of each entry to a string?


1 Answers

Iterators produced by a ConcurrentHashMap are weakly consistent. That is:

  • they may proceed concurrently with other operations
  • they will never throw ConcurrentModificationException
  • they are guaranteed to traverse elements as they existed upon construction exactly once, and may (but are not guaranteed to) reflect any modifications subsequent to construction.

The last bullet-point is pretty important, an iterator returns a view of the map at some point since the creation of the iterator, to quote a different section of the javadocs for ConcurrentHashMap:

Similarly, Iterators, Spliterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration.

So when you loop through a keyset like the following, you need to double check if the item still exists in the collection:

for(Integer i: indentificationDocuments.keySet()){
    // Below line could be a problem, get(i) may not exist anymore but may still be in view of the iterator
    // someStringBuilder.append(indentificationDocuments.get(i));
    // Next line would work
    someStringBuilder.append(identificationDocuments.getOrDefault(i, ""));
}

The act of appending all the strings to the StringBuilder itself is safe, as long as you are doing it on one thread or have encapsulated the StringBuilder entirely in a thread-safe manner.

like image 138
NESPowerGlove Avatar answered Jan 23 '26 05:01

NESPowerGlove