将list按指定大小分为n组

public static <T> List<List<T>> splitList(List<T> list,int groupSize){
List<List<T>> result = Lists.newArrayList();
int length = list.size();
//每组大小
StringBuilder stringBuilder = null;
if (length > groupSize){


int lastIndex = 0;
int fromIndex = 0;
int endIndex = 0;
//计算分多少组
int num = (length + groupSize -1 ) / groupSize;
for(int i = 0;i<num;i++){
//第i组的起始index
fromIndex = i *groupSize;
//第i组的结束index
endIndex = (i+1) * groupSize < length ? (i+1) * groupSize : length;

List<T> tempList = Lists.newArrayList();
for (int j = fromIndex;j<endIndex;j++){
tempList.add(list.get(j));
}


result.add(tempList);
}
}else{
result.add(list);
}
return result;
}
原文地址:https://www.cnblogs.com/heamin/p/14296432.html