Java ConcurrentHashMap and for each loop

风格不统一 提交于 2021-01-20 08:09:47

问题


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:


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.




回答2:


I don't know If you are really asking this, but to iterate over any map you iterate over the keySet()

StringBuffer result = new StringBuffer("");

for(Integer i: indentificationDocuments.keySet()){
        result.append(indentificationDocuments.get(i));
}

return result.toString();


来源:https://stackoverflow.com/questions/31275265/java-concurrenthashmap-and-for-each-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!