List 集合分批次处理
作者:互联网
一、代码
public class BatchProcess { public static void main(String[] args) { List<Integer> totalList = new ArrayList<>(); for (int i = 1; i <= 100; i++) { totalList.add(i); } batchProcessList(totalList); } // 将 totalList 集合分成多批进行处理(每一批 batchList) public static void batchProcessList(List<Integer> totalList) { Integer BATCH_COUNT = 20; List<Integer> batchList = new ArrayList<>(); if (!CollectionUtils.isEmpty(totalList)) { for (int i = 0; i < totalList.size(); i++) { batchList.add(totalList.get(i)); // 如果分批的集合填充满了元素或者填充的是最后一个元素 if ((Objects.nonNull(batchList) && Objects.equals(batchList.size(), BATCH_COUNT)) || Objects.equals((totalList.size() - 1), i)) { System.out.println(batchList); System.out.println("------------------------------------------"); // 执行完了处理逻辑之后,将 batchList 的元素清空 batchList.clear(); } } } } }
二、测试结果
标签:批次,totalList,List,System,Objects,集合,batchList,size 来源: https://www.cnblogs.com/xiaomaomao/p/15630060.html