Not sure what is triggering a java.util.ConcurrentModificationException
when I iterate over the LinkedHashMap
structure in the code below. Using the
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 argumentX
, then letI
be the typejava.util.Iterator
; otherwise, letI
be the raw typejava.util.Iterator
.The enhanced
for
statement is equivalent to a basicfor
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 ownremove
method, the iterator will throw aConcurrentModificationException
.
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);
}