I'm getting a ConcurrentModificationException while trying to remove an entry from a HashMap and I can't figure out why. I'm not iterating through it, I'm simply checking if it contains a given key and deleting the value mapped to it if it does. I have read questions on this, but all of them are people trying to modify the HashMap while iterating through it without using an iterator, which is not my case.
This is the code
serverTasksNotInDB = CompareLists.serverTasksNotInDB(localTasks, serverTasks);
if (serverTasksNotInDB != null && !serverTasksNotInDB.isEmpty()) {
for (Task serverTask : serverTasksNotInDB) {
String id = serverTask.getId();
mPresenter.addTaskFirstTimeFromServer(serverTask, currentListId);
if (serverTasks != null) {
serverTasks.remove(serverTask);
boolean containsKey = serverTasksMap.containsKey(id);
if (containsKey) {
//TODO Fix concurrency modification exception here (down)
serverTasksMap.remove(id);
}
}
}
}
for (Task serverTask : serverTasksNotInDB)
actually serverTasksNotInDB ArrayList call iterator() method.
foreach Api description.
Fail-fast operations throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: ConcurrentModificationException should be used only to detect bugs.