Why, with enhanced for loops in Java, can I not change what I am iterating through references to? Not the iterator, but the field of the data type I am iterating through.
I want to iterate through a list of Map
s(MultiValueMap
s to be precise), and check to see if the values of the map can be updated.
My problem is that the actual logic to change this is nested deep within other loops (outline code below) that are based off of the map, and I can't find a way to do the replace without ConcurrentModificationException
s being thrown. The reason is when I get ConcurrentModificationException
taken care of for one loop, it breaks a different one since the code is nested deep.
In the main loop, iterating through the maps, I want to create a new map, that I just add things to, and in the end replace the iterating map with it. But this doesn't work.
Why is this, and what I can I do or research to help me resolve this?
Pseudo Code
for( Map loopMap : listOfMaps ) {
Map tempMap = new HashMap();
Set entrySet = loopMap.entrySet();
Iterator<MultiValueMap.Entry> iter = entrySet.iterator();
while( iter.hasNext() ) {
Map.Entry entry = (Entry) iter.next();
if( entry.getValue() == "test" ) {
tempMap.put( entry.getKey(), "new value" );
}
}
loopMap = tempMap;
}
Why can I not change what the map refers to even though I am able to change what is refering to's value?