java.util.ConcurrentModificationException con iterador
Sé que si intentara eliminar de la recopilación en bucle a través de él con el bucle simple, obtendré esta excepción:java.util.ConcurrentModificationException
. Pero estoy usando Iterator y todavía me genera esta excepción. ¿Alguna idea de por qué y cómo resolverlo?
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);
}
}
}
}