java.util.ConcurrentModificationException with iterator
2022-09-01 08:43:21
我知道如果会尝试从集合中删除循环通过它的简单循环,我将获得这个异常:.但是我正在使用迭代器,它仍然会生成这个异常。任何想法为什么以及如何解决它?java.util.ConcurrentModificationException
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);
}
}
}
}