我有以下代码,它是较长代码段的一部分,旨在将对象图转换为平面树,以便现有代码可以进一步处理该树:
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));
}
如您所见,除了获取迭代器外,我从不与 projectTreeView.getProjectStreamList() 交互,而且我从不在任何列表上调用 .remove() 或 .add()。我所做的唯一改变事物的事情是树图上的 put() 和本地 HashSet() 上的 .add。我也不以任何方式更改原始 projectTreeView,并且我只读取使用迭代器检索的 projectStreamView 的属性。
RISEBY
相关分类