Java ConcurrentHashMap and for each loop

后端 未结 2 1203
离开以前
离开以前 2021-01-23 05:52

Supposed I have the following ConcurrentHashMap:

ConcurrentHashMap identificationDocuments = new ConcurrentHashMap

        
相关标签:
2条回答
  • 2021-01-23 06:13

    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();
    
    0 讨论(0)
  • 2021-01-23 06:19

    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.

    0 讨论(0)
提交回复
热议问题