package com.fh.util;
import java.util.ArrayList;
import java.util.List;
/**
* list 集合工具类
* @author yuer629
*
*/
public class ListUtils {
/**
* 拆分集合为多个子集合
* @param <T>
* @param sourceList 源集合list
* @param batchCount 每个子list的最大容量
* @return
*/
public static<T> List<List<T>> batchList(List<T> sourceList, int batchCount) {
List<List<T>> returnList = new ArrayList<>();
int startIndex = 0; // 从第0个下标开始
while (startIndex < sourceList.size()) {
int endIndex = 0;
if (sourceList.size() - batchCount < startIndex) {
endIndex = sourceList.size();
} else {
endIndex = startIndex + batchCount;
}
returnList.add(sourceList.subList(startIndex, endIndex));
startIndex = startIndex + batchCount; // 下一批
}
return returnList;
}
}