I have looked at other questions that deal with similar problems, but it seems that this particular case is rather unique.
The code below is supposed to go through a list of Events and put them in a map if their times overlap.
// identify events that occur at the same time
List<Event> remainingEvents = new LinkedList<>();
remainingEvents = Event.getAllEvents();
for (Event event1 : Event.getAllEvents()) {
for (Event event2 : remainingEvents) {
// check if it is the same event
if (event1 == event2)
continue;
// check whether they overlap
if (event1.overlapsWith(event2)) {
simultEvents.put(event1, event2);
}
}
// event1 is now exhausted
remainingEvents.remove(event1);
}
Notice that I am modifying the remainingEvents
list after the iteration is complete, not during. Therefore, I do not understand why I am getting a ConccurentModificationException.
Elipse also tells me that the error occurs on this line:
for (Event event1 : Event.getAllEvents()) {
which makes no sense since I am not modifying anything relating to it.
Note that this is not a duplicate because I am not modifying the list until I am done Iterating with it (unlike other questions), and the error occurs on a line that has nothing to do with the removal