之所以写这个工具类,是产品上关于巨量数据导出的一个需求,有时间复杂度更低的写法欢迎来一起讨论,废话不说来看代码吧
/**
* List<> 根据输入的组内个数进行分组
* @param c 组内数量
* @author chengmeng
*/
public static <T> List<List<T>> listTransGroup(int c,List<T> lst) {
List<List<T>> result = new ArrayList<List<T>>();
int m = lst.size()%c;
int n = lst.size()/c;
for (int i = 1; i <= n ; i++) {
List<T> temp = new ArrayList<T>();
for (int j = 0; j < c ; j++) {
temp.add(lst.get(j+(i-1)*c));
}
result.add(temp);
}
if(m > 0) {
List<T> temp = new ArrayList<T>();
for (int i = 0; i < m ; i++) {
temp.add(lst.get(n*c+i));
}
result.add(temp);
}
return result;
}