首页 > 解决方案 > 在 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 是否共享迭代器状态或其他东西?

标签: javaiterator

解决方案


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


推荐阅读