1

i have the following code:

public List<String> processMap(Map<String, String> aMap) {
    Cloner cloner = new Cloner();

    Map<String, String> tempMap = cloner.deepClone(aMap);

    while(!tempMap.isEmpty()) {
        Iterator<Entry<String, String>> iterator = tempMap.entrySet().iterator();

        while(iterator.hasNext()) {
            Entry<String, String> entry = iterator.next(); // !!!
        }
    }

    return null;
}

To deep copy the map i use this library: Cloner

I marked the line where i unfortunately get a ava.util.ConcurrentModificationException with '!!!'

Can you please tell me why i get this exception?

The complete code:

    Cloner cloner = new Cloner();

    Map<String, FreebaseType> tempFreebaseTypes = new HashMap<String, FreebaseType>();
    Map<String, FreebaseType> freebaseTypesCopy = cloner.deepClone(freebaseTypes);

    while(!freebaseTypesCopy.isEmpty()) {
        Iterator<Entry<String, FreebaseType>> iterator = freebaseTypesCopy.entrySet().iterator();

        while(iterator.hasNext()) {
            Entry<String, FreebaseType> entry = iterator.next();

            if(tempFreebaseTypes.containsKey(entry.getValue().getSuperType()) || entry.getValue().getSuperType() == null) {
                tempFreebaseTypes.put(entry.getValue().getType(), entry.getValue());

                freebaseTypesCopy.remove(entry.getKey());
            } 
        }
    }

    List<FreebaseType> sortedFreebaseTypes = new ArrayList<FreebaseType>();
    Iterator<Entry<String, FreebaseType>> iterator = tempFreebaseTypes.entrySet().iterator();

    while(iterator.hasNext()) {
        Entry<String, FreebaseType> entry = iterator.next();

        sortedFreebaseTypes.add(entry.getValue());
    }

    return sortedFreebaseTypes;
15
  • Can you pls not abbreviate "please"? Commented Apr 23, 2014 at 15:15
  • not sure . some thing like . iterator.remove(obj) in for loop Commented Apr 23, 2014 at 15:18
  • I would guess that Cloner clones parts of the implementation of the map which aren't supposed to be cloned which results in a corrupted Map. Commented Apr 23, 2014 at 15:19
  • 1
    What else is in your while loop? Can you come up with a short but complete program demonstrating the problem? Commented Apr 23, 2014 at 15:19
  • 1
    When it's a map of <String, String> why do you use an external library anyway? You can copy the content of one map into another with newMap.putAll(originalMap) Commented Apr 23, 2014 at 15:22

1 Answer 1

2

While you iterate a map with an iterator, you are not allowed to modify the map directly, or you get a ConcurrentModificationException. The only way to modify the map is indirectly through the iterators remove() method, which removes the entry the iterator is currently pointing to from the map.

Replace the line

freebaseTypesCopy.remove(entry.getKey());

with

iterator.remove();

and the code should work.

Sign up to request clarification or add additional context in comments.

6 Comments

So how can i actually add/remove elements in my List, Map or whatever while im iterating it? I need to do it.
As I wrote, you can remove the current element through the iterators remove-method. But you can not add any elements to the currently iterated collection and neither can you remove elements the iterator is not currently pointing to.
The documentation says: "void remove(): Removes from the underlying collection the last element returned by this iterator." What else do you think this method could do?
@Mulgard When your program runs in an infinite loop, I would rather suspect a bug in the if-condition before the remove which is never satisfied for some elements, so they won't ever get deleted. That would mean that while(!freebaseTypesCopy.isEmpty()) will never terminate. Did you trace your code with the debugger?
@Mulgard Why do you have the remove in the if-condition in the first place? Either you want to remove all elements from freebaseTypesCopy, then putting the remove into the if-condition would be pointless, or you don't want to remove all elements, then the while(!freebaseTypesCopy.isEmpty()) loop would be counter-productive.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.