一.List拆分
1.遍历集合for循环
/**
* for循环将大集合拆为多个小集合
*
* @param list
*/
public static <T> List<T> splitList(List<T> list) {
List<T> numList = new LinkedList<>();
List<T> innerNumList = new LinkedList<>();
for (int i = 0; i < list.size(); i++) {
innerNumList.add(list.get(i));
if (innerNumList.size() == 10) { // 10 小集合的数 可修改
numList.addAll(innerNumList);
innerNumList = new LinkedList<>();
}
}
return numList;
}
2.不遍历集合的for循环
/**
* 利用分页模式拆分小集合,不用遍历集合
*
* @param list
*/
public static <T> List<T> splitList2(List<T> list) {
List<T> numList = new LinkedList<>();
int total = list.size(); // 总数
int perTotal = 1000; // 每次执行数据
int count = (total + perTotal - 1) / perTotal;
for (int i = 0; i < count; i++) {
int fromIndex = i * perTotal;
// int toIndex =(i+1)*perTotal >=total ? total:(i+1)*perTotal; 用注释的这种模式也可以
int toIndex = Math.min(((i + 1) * perTotal), total);
numList.addAll(list.subList(fromIndex, toIndex).stream().toList());
}
return numList;
}