有时候业务场景集合的数量很大,如果单处理的话耗时严重,所以可以进行集合的分割,然后使用多线程的方式进行批量处理,节约时间。
public static <T> List<List<T>> splitList(List<T> list, int groupSize){
int length = list.size();
/**
* num 可以分成的组数
**/
int num = ( length + groupSize - 1 )/groupSize ;
//用于存放最后结果
List<List<T>> result= new ArrayList<>(num);
for (int i = 0; i < num; i++) {
// 开始位置
int fromIndex = i * groupSize;
// 结束位置
int toIndex = (i+1) * groupSize < length ? ( i+1 ) * groupSize : length ;
result.add(list.subList(fromIndex,toIndex)) ;
}
return result;
}