I'm getting a Concurrent Modification Exception when running the for each loop of the following code:
if (entityList.isEmpty()) {
entityList.add(entity);
}
else {
for (Entity e: entityList) {
if (e.getName().equals(p.toString())) {
e.setOccurrence(e.getOccurrence() + 1);
}
else {
entityList.add(entity);
}
}
}
This happens because I try to read from the entityList and write to it in the same thread, right? I'm not sure how to resolve the issue for working with an Iterator object only seems to make sense if the exception occurs when removing list items.