package cn.tk.netcore.rest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Listgrop{
//方法二:效率比较高 备注:pageSize集合显示的分组个数,soruceDatas为集合原数据
private static List<List<String>> groupByList(List<String> sourceDatas,int pageSize) {
// 分组集合
List<List<String>> groupLists = new ArrayList<> ();
// 分组数据
List<String> tempDatas = new ArrayList<> ();
if (sourceDatas != null && pageSize > 0) {
for (int i = 0;i < sourceDatas.size ();i++) {
if (i % pageSize == 0) {
if (!tempDatas.isEmpty ()) {
groupLists.add (tempDatas);
}
tempDatas = new ArrayList<> ();
tempDatas.add (sourceDatas.get (i));
} else {
tempDatas.add (sourceDatas.get (i));
}
}
}
if (!tempDatas.isEmpty ()) {
groupLists.add (tempDatas);
}
return groupLists;
}
public static void main(String[] args) {
// 分组记录数
int pageSize = 5;
// 原始数据
List<String> list = new ArrayList<String> (Arrays.asList ("a","b","c",
"d","e","f","g","h"));
System.out.println (groupByList (list,pageSize));
}
}