private List<DlLineage> getLineageListBySourceTableIds(String sourceSysName, String bizName, List<Long> sourceTableIds) { List<DlLineage> results = new ArrayList<>(); if (CollectionUtils.isEmpty(sourceTableIds)) { return results; } DlLineage lineage = new DlLineage(); lineage.setSourceSysName(sourceSysName); lineage.setBizName(bizName); sourceTableIds.forEach(sourceTableId -> { lineage.setSourceTableId(sourceTableId); String ids = dlLineageMapper.queryChildrenLineageBySourceIdSysNameBizNameIds(lineage); if (StringUtils.isNotEmpty(ids)) { results.addAll(dlLineageMapper.selectListByIds(ids)); } }); // 将targetLayer中的api层单抽出来作为一个对象进行add添加 for (DlLineage result : results) { DlLineage dlLineage = new DlLineage(); if (result.getTargetLayer().equals(LayerTypeEnum.APILAYER.getCode())) { dlLineage.setSourceTableId(result.getTargetTableId()); dlLineage.setSourceSysName(result.getTargetSysName()); dlLineage.setSourceTableName(result.getSourceTableName()); } results.add(dlLineage); } return results; }代码中 for (DlLineage result : results)出现java.util.ConcurrentModificationException: null并解决
时间: 2024-04-15 19:30:13 浏览: 131
出现`java.util.ConcurrentModificationException`异常是因为在迭代`results`列表的同时,又对列表进行了修改。在代码中,使用了增强型for循环来遍历`results`列表,并在循环体内对列表进行了添加操作,导致了异常的发生。
要解决这个问题,你可以使用一个临时的列表来保存需要添加的对象,然后在循环结束后统一将临时列表中的对象添加到`results`列表中。以下是修改后的代码示例:
```java
private List<DlLineage> getLineageListBySourceTableIds(String sourceSysName, String bizName, List<Long> sourceTableIds) {
List<DlLineage> results = new ArrayList<>();
if (CollectionUtils.isEmpty(sourceTableIds)) {
return results;
}
DlLineage lineage = new DlLineage();
lineage.setSourceSysName(sourceSysName);
lineage.setBizName(bizName);
sourceTableIds.forEach(sourceTableId -> {
lineage.setSourceTableId(sourceTableId);
String ids = dlLineageMapper.queryChildrenLineageBySourceIdSysNameBizNameIds(lineage);
if (StringUtils.isNotEmpty(ids)) {
results.addAll(dlLineageMapper.selectListByIds(ids));
}
});
// 临时列表用于保存需要添加的对象
List<DlLineage> tempResults = new ArrayList<>();
for (DlLineage result : results) {
if (result.getTargetLayer().equals(LayerTypeEnum.APILAYER.getCode())) {
DlLineage dlLineage = new DlLineage();
dlLineage.setSourceTableId(result.getTargetTableId());
dlLineage.setSourceSysName(result.getTargetSysName());
dlLineage.setSourceTableName(result.getSourceTableName());
tempResults.add(dlLineage);
}
}
// 将临时列表中的对象添加到results列表中
results.addAll(tempResults);
return results;
}
```
通过使用临时列表来保存需要添加的对象,可以避免在迭代`results`列表时对其进行修改,从而解决了`ConcurrentModificationException`异常的问题。
阅读全文