迭代列表时运行java.util.ConcurrentModificationException

时间:2013-05-17 21:27:48

标签: java list concurrentmodification

我正在尝试将记录列表拆分为记录的子列表。我成功地将列表拆分为子列表,我想看到子列表的内容,但不知怎的,我一直在遇到这个ConcurrentModificationException。

我的分割方法:

/**
     * @param list - list of results
     * @param size - how many sublists
     * @return ret - returns a list containing the sublists
     * */
    public static <T> List<List<T>> split(List<T> list, int size) throws NullPointerException, IllegalArgumentException {
        if (list == null) {
            throw new NullPointerException("The list parameter is null.");
        }
        if (size <= 0) {
            throw new IllegalArgumentException("The size parameter must be more than 0.");
        }

        int recordsPerSubList = list.size() / size; // how many records per sublist
        List<List<T>> sublists = new ArrayList<List<T>>(size); // init capacity of sublists

        // add the records to each sublist
        for (int i=0; i<size; i++) {
            sublists.add(i, list.subList(i * recordsPerSubList, (i + 1) * recordsPerSubList));
        }

        // for the remainder records, just add them to the last sublist
        int mod = list.size() % recordsPerSubList;
        if (mod > 0) {
            int remainderIndex = list.size() - mod;
            sublists.get(size - 1).addAll(list.subList(remainderIndex, list.size()));
        }

        return sublists;
    }

我在这里打电话:

List<List<QuoteSearchInfo>> ret = Util.split(quoteSearchInfoList, 5);

            int fileCounter = 0;
            for (List<QuoteSearchInfo> sublist : ret) {
                fileCounter++;

                String sublistJson = new Gson().toJson(sublist);
                filename = JSON_FILE_NAME + fileCounter + JSON_FILE_END;
                saveToFile(filename, sublistJson);
                AWSManager.getInstance().uploadQuoteSearchJson(filename);
            }

^这里我试图将列表拆分为子列表,以便我可以将它们上传到S3。

和堆栈跟踪:

java.util.ConcurrentModificationException 
at java.util.SubList.checkForComodification(AbstractList.java:752) 
at java.util.SubList.listIterator(AbstractList.java:682) 
at java.util.AbstractList.listIterator(AbstractList.java:284) 
at java.util.SubList.iterator(AbstractList.java:678) 
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:95) 
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60) 
at com.google.gson.Gson.toJson(Gson.java:546)
at com.google.gson.Gson.toJson(Gson.java:525) 
at com.google.gson.Gson.toJson(Gson.java:480) 
at com.google.gson.Gson.toJson(Gson.java:460) 
at com.crover.QuoteSearchRover.execute(QuoteSearchRover.java:41) 
at com.crover.CroverMain.execute(CroverMain.java:85) 
at com.crover.CroverMain.main(CroverMain.java:35)

1 个答案:

答案 0 :(得分:6)

sublists.get(size - 1).addAll(list.subList(remainderIndex, list.size()));

sublists.get(size - 1)是列表子范围的实时视图,因此当您向其添加元素时,您也会在原始列表中添加元素,同时您还要尝试获取来自list.subList的元素。通常,在迭代时不能修改列表。

最简单的解决方案是不将元素添加到sublists.get(size - 1),而只是更新它:

sublists.set(size - 1, 
   list.subList(remainderIndex - recordsPerSublist, list.size()));
相关问题