1、工具类
/**
* 按照条数对List进行分组
*
* @param list 目标集合
* @param toIndex 多少条为一组
* @return
*/
public static Map groupList(List list,int toIndex){
int listSize=list.size();
Map map = new HashMap(); //用map存起来新的分组后数据
int keyToken = 0;
for(int i = 0;i<list.size();i+=toIndex){
if(i+toIndex>listSize){ //剩余几条newList中就装几条
toIndex=listSize-i;
}
List newList = list.subList(i,i+toIndex);
map.put("result"+keyToken, newList);
keyToken++;
}
return map;
}
2、测试方法
public static void main(String[] args) {
List<MyLjyyxJdjhbShdw> a = new ArrayList<MyLjyyxJdjhbShdw>();
MyLjyyxJdjhbShdw b1 = new MyLjyyxJdjhbShdw();
b1.setJdjhbBh("1");
a.add(b1);
MyLjyyxJdjhbShdw b2 = new MyLjyyxJdjhbShdw();
b2.setJdjhbBh("2");
a.add(b2);
MyLjyyxJdjhbShdw b3 = new MyLjyyxJdjhbShdw();
b3.setJdjhbBh("3");
a.add(b3);
MyLjyyxJdjhbShdw b4 = new MyLjyyxJdjhbShdw();
b4.setJdjhbBh("4");
a.add(b4);
MyLjyyxJdjhbShdw b5 = new MyLjyyxJdjhbShdw();
b5.setJdjhbBh("5");
a.add(b5);
MyLjyyxJdjhbShdw b6 = new MyLjyyxJdjhbShdw();
b6.setJdjhbBh("6");
a.add(b6);
MyLjyyxJdjhbShdw b7 = new MyLjyyxJdjhbShdw();
b7.setJdjhbBh("7");
a.add(b7);
MyLjyyxJdjhbShdw b8 = new MyLjyyxJdjhbShdw();
b8.setJdjhbBh("8");
a.add(b8);
MyLjyyxJdjhbShdw b9 = new MyLjyyxJdjhbShdw();
b9.setJdjhbBh("9");
a.add(b9);
MyLjyyxJdjhbShdw b10 = new MyLjyyxJdjhbShdw();
b10.setJdjhbBh("10");
a.add(b10);
Map<String,Object> map = groupList(a,3);
for (Map.Entry<String,Object> entry : map.entrySet()) {
List<MyLjyyxJdjhbShdw> c = (List<MyLjyyxJdjhbShdw>) entry.getValue();
for (MyLjyyxJdjhbShdw myLjyyxJdjhbShdw : c) {
System.out.println(myLjyyxJdjhbShdw.getJdjhbBh());
}
}
}