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; } } }