在Arraylist.iterator()。next()上获取ConcurrentModificationException,但我没有更改原始列表

时间:2019-08-13 09:49:52

标签: java iterator

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

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是否共享迭代器状态或其他内容?

1 个答案:

答案 0 :(得分:0)

我做了一些检查,发现是什么原因造成的。原来projectTreeView.getLevelList()中存在错误。在使用Collections.sort()返回列表之前,我对列表进行了排序,而不是在那里对levelList进行排序,而是对projectStreamList ...

进行了排序