java - 在 Arraylist.iterator().next() 上获取 ConcurrentModificationException,但我没有更改原始列表
问题描述
我有以下代码,它是较长代码部分的一部分,旨在将对象图转换为平面树,以便可以通过现有代码进一步处理该树:
private void addProjectStreamsFromGraphToTree(String parentIndex, Map<String, Object> objectTree, ProjectTreeView projectTreeView) {
Set<Integer> allConnectedLevelOids = new HashSet<Integer>();
// Set in the tree + add everything underneath projectStream:
int index = 1;
String treeIndex ="";
for (Iterator<ExportableProjectStreamView> iter = projectTreeView.getProjectStreamList().iterator(); iter.hasNext(); index++) {
// Getting the concurrentModificationException on the below line
ProjectStreamView projectStreamView = (ProjectStreamView) iter.next();
treeIndex = parentIndex + "." + index;
objectTree.put(treeIndex, projectStreamView);
addLifeCyclesFromGraphToTree(allConnectedLevelOids, projectStreamView , treeIndex, objectTree, projectTreeView);
}
}
private void addLifeCyclesFromGraphToTree(Set<Integer> allConnectedLevelOids, ProjectStreamView projectStreamView, String parentIndex, Map<String, Object> objectTree, ProjectTreeView projectTreeView) {
int index = 1;
String treeIndex ="";
ExportableLifecycleView lifecycleView = projectTreeView.getLifecycleList().stream().filter(lcv -> projectStreamView.getLifecycleOid().equals(lcv.getOid())).findFirst().get();
treeIndex = parentIndex + "." + index;
objectTree.put(treeIndex, lifecycleView);
// add the levels that are linked to this lifecycle and at once add all oids of the levels,
// connected to this lifecycle, to the allconnectedLevelOids.
allConnectedLevelOids.addAll(addConnectedLevelsFromGraphToTree(lifecycleView, treeIndex, objectTree, projectTreeView));
}
protected Set<Integer> addConnectedLevelsFromGraphToTree(ExportableLifecycleView lifecycleView, String parentIndex, Map<String, Object> objectTree, ProjectTreeView projectTreeView) {
Set<Integer> connectedLevelOids = new HashSet<Integer>();
int index = 1;
String treeIndex ="";
for (Iterator<ExportableLifecycleAssociationView> iter = lifecycleView.getLifecycleAssociations().iterator(); iter.hasNext(); index++) {
ExportableLifecycleAssociationView exportableLifecycleAssociationView = iter.next();
treeIndex = parentIndex + "." + index;
objectTree.put(treeIndex, exportableLifecycleAssociationView);
ExportableLevelView connectedLevelView = projectTreeView.getLevelList().stream().filter(level -> level.getOid().equals(exportableLifecycleAssociationView.getLevelOid())).findFirst().get();
index++;
treeIndex = parentIndex + "." + index;
objectTree.put(treeIndex, connectedLevelView);
// add the levels to the returning hashset:
connectedLevelOids.add(connectedLevelView.getOid());
}
return connectedLevelOids;
}
如您所见,除了获取迭代器外,我从不与 projectTreeView.getProjectStreamList() 交互,并且我从不在任何列表上调用 .remove() 或 .add() 。我所做的唯一改变事情的事情是在树形图上放置()和在本地 HashSet()上添加 .add。我也不会以任何方式更改原始 projectTreeView,我只读取使用迭代器检索的 projectStreamView 上的属性。
不过,我收到以下错误:
java.util.ConcurrentModificationException: null
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909) ~[?:1.8.0_172]
at java.util.ArrayList$Itr.next(ArrayList.java:859) ~[?:1.8.0_172]
at be.ikan.scm4all.business.server.bs.clone.ProjectImportExportServiceImpl.addProjectStreamsFromGraphToTree(ProjectImportExportServiceImpl.java:615) ~[classes/:?]
at be.ikan.scm4all.business.server.bs.clone.ProjectImportExportServiceImpl.getProjectTreeFromGraph(ProjectImportExportServiceImpl.java:604) ~[classes/:?]
at be.ikan.scm4all.business.server.bs.clone.ProjectImportExportServiceImpl.importProject(ProjectImportExportServiceImpl.java:515) ~[classes/:?]
at be.ikan.scm4all.client.rest.controller.projectimportexport.ProjectImportRestController.importProject(ProjectImportRestController.java:110) ~[classes/:?]
我在这里打破了其他一些迭代器规则吗?即使我没有更改原始列表,为什么会出现此异常?这似乎与 addConnectedLevelsFromGraphToTree() 中的迭代器有关,但它在树的不同部分迭代了一个完全不同的列表。ArrayLists 是否共享迭代器状态或其他东西?
解决方案
我做了一些检查,我发现了它的原因。原来,在projectTreeView.getLevelList()
. 我在使用返回之前对列表进行Collections.sort()
排序,而不是在levelList
那里排序,我排序projectStreamList
...
推荐阅读
- optimization - 在 gurobipy 中使用再平衡来建模投资组合优化的问题
- python - Pytesseract 问题
- linux - 最小汇编程序ARM
- python - 从 php 执行 python 脚本时导入失败
- php - WP_Query 无法在两个日期之间进行过滤
- r - 带有插入符号的 glmnet 返回“lognet 中的错误(x、is.sparse、ix、jx、y、权重、偏移量、alpha、nobs、:外部函数调用中的 NA/NaN/Inf(参数 5)”
- php - 如何以对象类型作为输出参数调用 Oracle 存储过程?(PLS-00306:调用中的参数数量或类型错误)
- ruby-on-rails - 无法提取 RVM 源
- amazon-web-services - AWS CLI 命令以表格/文本格式获取 AMI 的图像大小
- vba - 运行时错误 15 - 代码类型不匹配以更改 MS Access 数据库中的密码