Java list分批处理数据之subList()

   public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 1000; i++) {
            list.add(i);
        }
        System.out.println(list.size());
        //注意:subList(),会修改原有list,如果后续代码需要用list的数据,需要慎重!!!
        while (true) {
            System.out.println(list.size());
            if (list != null && list.size() > 300) {
                List<Integer> newList = list.subList(0, 300);
                System.out.println(newList.size());
                //很重要!!!
                newList.clear();
            } else {
                System.out.println(list.size());
                break;
            }
        }

    }

 

posted @ 2021-08-11 10:52  不忘初心5470  阅读(565)  评论(0编辑  收藏  举报