ConcurrentModificationException with LinkedHashMap

后端 未结 6 664
北恋
北恋 2021-02-04 03:09

Not sure what is triggering a java.util.ConcurrentModificationException when I iterate over the LinkedHashMap structure in the code below. Using the

6条回答
  •  野性不改
    2021-02-04 03:21

    Read the Javadoc for LinkedHashMap:

    A structural modification is any operation that adds or deletes one or more mappings or, in the case of access-ordered linked hash maps, affects iteration order. In insertion-ordered linked hash maps, merely changing the value associated with a key that is already contained in the map is not a structural modification. In access-ordered linked hash maps, merely querying the map with get is a structural modification.

    Since you're passing in true to the LinkedHashMap constructor, it is in access order and when you are trying to get something from it, you are structurally modifying it.

    Also note that when you use the enhanced for syntax, you are actually using an iterator. Simplified quote from JLS §14.14.2:

    The enhanced for statement has the form:

    EnhancedForStatement:
    
    for ( TargetType Identifier : Expression ) Statement
    

    [...]

    If the type of Expression is a subtype of Iterable for some type argument X, then let I be the type java.util.Iterator; otherwise, let I be the raw type java.util.Iterator.

    The enhanced for statement is equivalent to a basic for statement of the form:

    for (I #i = Expression.iterator(); #i.hasNext(); ) {
         TargetType Identifier =
             (TargetType) #i.next();
         Statement
    }
    

    #i is an automatically generated identifier that is distinct from any other identifiers (automatically generated or otherwise) that are in scope (§6.3) at the point where the enhanced for statement occurs.

    Also, in the Javadoc for LinkedHashMap:

    The iterators returned by the iterator method of the collections returned by all of this class's collection view methods are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.

    Therefore, when you are calling get on the map, you are performing structural modifications to it, causing the iterator in the enhanced-for to throw an exception. I think you meant to do this, which avoids calling get:

    for (Integer i : lru_cache.values()) {
        System.out.println(i);
    }
    

提交回复
热议问题