package com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ch.qos.logback.core.net.SyslogOutputStream;
public class TestList {
public static void main(String[] args) {
List tarArr = new ArrayList();
for(int i=0; i<10; i++){
User user = new User();
user.setId(i);
user.setName("小明"+i);
tarArr.add(user);
}
List> result = createList(tarArr, 3);
System.out.println(result);
}
/**
*
* @param targe 将要被拆分的list
* @param size 把将要拆分的list拆分成目标list的个数
* @return
*/
public static List createList(List targe,int targeSplitSize){
List resultList = new ArrayList>() ;
//先求每组平均分多少个,如果不够分补足,最后一个少分
int avgSize = targe.size()%targeSplitSize==0?targe.size()/targeSplitSize:targe.size()/targeSplitSize+1;
//求出最后一组的个数
int lastArraySize = targe.size() - avgSize*(targeSplitSize-1);
int k = 0; //取出list中元素的计数器
for(int i=0; i
List list = new ArrayList();
//如果不是最后一组则按照平均大小拆分目标list
if(i !=(targeSplitSize-1)){
for(int j=0; j
User user = targe.get(k);
list.add(user);
k++;
}
}else{
for(int j=0; j
User user = targe.get(k);
list.add(user);
k++;
}
}
resultList.add(list);
}
return resultList;
}
}