1 package com.example.demo; 2 import com.google.common.collect.Lists; 3 import java.util.ArrayList; 4 import java.util.List; 5 public class Test { 6 public static void main(String[] args) { 7 List<Integer> list = new ArrayList<>(); 8 for (int i = 0; i < 1500; i++) { 9 list.add(i); 10 } 11 newBatchProcessing(list); 12 //谷歌工具包里的 Lists.partition 13 oldBatchProcessing(list); 14 //传统切割 15 } 16 public static void newBatchProcessing(List<Integer> numList) { 17 List<List<Integer>> lists = Lists.partition(numList, 1000); 18 for (List<Integer> list : lists) { 19 System.out.println(list); 20 //这里就可以分批对数据进行处理了 21 } 22 } 23 /** 24 * 传统方法分批 25 */ 26 public static void oldBatchProcessing(List<Integer> list){ 27 int listSize = list.size(); 28 int toIndex = 1000; 29 for (int i = 0; i< list.size(); i += 1000){ 30 if(i + 1000 > listSize){ 31 //作用为toIndex最后没有900条数据则剩余几条newList中就装几条 32 toIndex = listSize - i; 33 } 34 List<Integer> newList = list.subList(i, i + toIndex); 35 //分批数据做具体处理 36 System.out.println(newList); 37 } 38 } 39 }
标签:toIndex,java,List,list,分批,public,1000 From: https://www.cnblogs.com/liulsky/p/17447661.html