一个list分割成若干个list的方法
作者:互联网
- 按指定大小,分隔集合,将集合按规定个数分为n个部分
public static List<List<T>> splitList(List<T> list, int len) {
if (list == null || list.size() == 0 || len < 1) {
return null;
}
List<List<T>> result = new ArrayList<List<T>>();
int size = list.size();
int count = (size + len - 1) / len;
for (int i = 0; i < count; i++) {
List<T> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
result.add(subList);
}
return result;
}
2.将一个list均分成n个list
public static <T> List<List<T>> averageAssign(List<T> list,int n){
List<List<T>> result=new ArrayList<List<T>>();
int remaider=list.size()%n; //(先计算出余数)
int number=list.size()/n; //然后是商
int offset=0;//偏移量
for(int i=0;i<n;i++){
List<T> value=null;
if(remaider>0){
value=list.subList(i*number+offset, (i+1)*number+offset+1);
remaider--;
offset++;
}else{
value=list.subList(i*number+offset, (i+1)*number+offset);
}
result.add(value);
}
return result;
}
标签:分割,int,list,List,len,offset,若干个,size 来源: https://blog.csdn.net/hhuiyao/article/details/121723373