数据分批 将一个list 按照一定条数拆分成几个list存放在list里
作者:互联网
import java.util.ArrayList;
import java.util.List;
public class SplitBatch {
public static <T> List<List<T>> split(List<T> resList, int count) {
if (resList == null || count < 1)
return null;
List<List<T>> ret = new ArrayList<List<T>>();
int size = resList.size();
if (size <= count) {
// 数据量不足count指定的大小
ret.add(resList);
} else {
int pre = size / count;
int last = size % count;
// 前面pre个集合,每个大小都是count个元素
for (int i = 0; i < pre; i++) {
List<T> itemList = new ArrayList<T>();
for (int j = 0; j < count; j++) {
itemList.add(resList.get(i * count + j));
}
ret.add(itemList);
}
// last的进行处理
if (last > 0) {
List<T> itemList = new ArrayList<T>();
for (int i = 0; i < last; i++) {
itemList.add(resList.get(pre * count + i));
}
ret.add(itemList);
}
}
return ret;
}
}
标签:count,int,分批,list,条数,itemList,resList,List,size 来源: https://blog.csdn.net/qq_34346915/article/details/91042614