gpt4 book ai didi

java - 在 Arraylist.iterator().next() 上遇到 ConcurrentModificationException,但我没有更改原始列表

转载 作者:行者123 更新时间:2023-12-02 01:43:31 24 4
gpt4 key购买 nike

我有以下代码,它是较长代码部分的一部分,旨在将对象图转换为平面树,以便可以通过现有代码进一步处理该树:

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() 。我所做的唯一改变的事情是在树形图上 put() 和在本地 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() 中的迭代器有关,但该迭代器迭代树的不同部分中的完全不同的列表。 ArrayList 共享迭代器状态还是其他什么?

最佳答案

我做了一些检查,找到了原因。原来projectTreeView.getLevelList()有一个bug 。在返回列表之前,我使用 Collections.sort() 对列表进行排序。 ,而不是排序 levelList在那里,我排序projectStreamList ...

关于java - 在 Arraylist.iterator().next() 上遇到 ConcurrentModificationException,但我没有更改原始列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57475220/

24 4 0