java.util.ConcurrentModificationException с итератором
Я знаю, если будет пытаться удалить из коллекции, проходя через него с простым циклом, я получу это исключение:java.util.ConcurrentModificationException
, Но я использую Iterator, и он все еще генерирует мне это исключение. Есть идеи почему и как это решить?
HashSet<TableRecord> tableRecords = new HashSet<>();
...
for (Iterator<TableRecord> iterator = tableRecords.iterator(); iterator.hasNext(); ) {
TableRecord record = iterator.next();
if (record.getDependency() == null) {
for (Iterator<TableRecord> dependencyIt = tableRecords.iterator(); dependencyIt.hasNext(); ) {
TableRecord dependency = dependencyIt.next(); //Here is the line which throws this exception
if (dependency.getDependency() != null && dependency.getDependency().getId().equals(record.getId())) {
tableRecords.remove(record);
}
}
}
}