Search code examples
javahashmapconcurrentmodification

A ConcurrentModificationException thrown without a modification


I have the following Java Code:

HashMap<Integer, Lesson> overflow = new HashMap<Integer, Lesson>();
HashMap<Integer, Lesson> new_lessons = this.lessons;
HashMap<Integer, Lesson> lessons = this.lessons;
for (Integer lesson : lessons.keySet()) {
    if(lessons.get(lesson).getLength().equals(LessonLength.DOUBLE)){
        if(lessons.containsKey(lesson + 1)){
            overflow.put(lesson + 1, lessons.get(lesson + 1));
            new_lessons.put(lesson+1, lessons.get(lesson));
            new_lessons.get(lesson).setLength(LessonLength.ONCE);
            new_lessons.get(lesson+1).setLength(LessonLength.ONCE);
        }
        else{
            new_lessons.put(lesson+1, lessons.get(lesson));
            new_lessons.get(lesson).setLength(LessonLength.ONCE);
            new_lessons.get(lesson+1).setLength(LessonLength.ONCE);
        }
    }
}

Why is there thrown an ConcurrentModificationException?


Solution

  • Your new_lessons and lessons variable have the same value - they're referring to the same object. So anything like this:

    new_lessons.put(lesson+1, lessons.get(lesson));
    

    ... is modifying lessons, which you're iterating over (via its key-set). Hence the problem.

    It's possible that if you didn't change the key set (i.e. you only changed the value associated with any entry) then you could be okay, but that's clearly not the case, because if lessons doesn't contain a key for lesson + 1, you're adding it.

    It sounds like you should probably copy the original map for new_lessons, so that you've got two independent maps. Or more simply, just take a copy of the keys to start with:

    List<Integer> keys = new ArrayList<Integer>(lessons.keySet());
    for (Integer lesson : keys) {
        ...
    }
    

    ... and get rid of your new_lessons variable as it's basically pointless.