java List集合分组实现方案

发布时间:2026/5/15 19:17:21

java List集合分组实现方案 1、实现方案1ListBiProductDto productDtoList; ListListBiProductDto splitListByStream splitListByStream(productDtoList,200); public static ListListBiProductDto splitListByStream(ListBiProductDto list, int size) { int limit (list.size() size - 1) / size; return Stream.iterate(0, n - n 1) .limit(limit) .map(i - list.subList(i * size, Math.min((i 1) * size, list.size()))) .collect(Collectors.toList()); }2、实现方案2import com.google.common.collect.Lists; ListLong showIds; log.info(productSaleStatusAndClassify showIds size is {},showIds.size()); // 分批同步防止场次数据过多 ListListLong partition Lists.partition(showIds, 200)3、实现方案3BatchSaveUtils.batchSave(ticketCheckLogBeanList,500,t - { ListLong delIdList new ArrayList(); //do something }); import java.util.List; import java.util.function.Consumer; public class BatchSaveUtils { public static int BATCH_NUM 5000; public BatchSaveUtils() { } public static T void batchSave(ListT l, int perCount, ConsumerListT c) { if (perCount 0) { throw new IllegalArgumentException(perCount 必须大于0); } else { for(int startIndex 0; startIndex l.size(); startIndex perCount) { int endIndex startIndex perCount l.size() ? l.size() : startIndex perCount; c.accept(l.subList(startIndex, endIndex)); } } } }

相关新闻