package test.transsion;import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class ListGroupTest { /* * 方法一:效率比较低 * public static void main(String[] args) { * //分组记录数 * int pageSize=5; * // 原始数据 * List list=newArrayList (Arrays.asList("a","b","c","d","e","f","g","h")); * //分组数据 * List > listGroup=new ArrayList >(); * //重新组装数据 * int groupSize=0; * if(list !=null && list.size()>0){ * if(list.size()% pageSize ==0){ * groupSize=list.size()/pageSize; * }else{ * groupSize=list.size()/pageSize+1; * } * } * for(int j=0;j dataList=new ArrayList<>(); * int temp=pageSize*(j+1); * if(temp>list.size()){ * temp=list.size(); * } * for(int i=pageSize*j;i > groupByList(List sourceDatas, int pageSize) { // 分组集合 List > groupLists = new ArrayList >(); // 分组数据 List 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 list = new ArrayList (Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h")); System.out.println(groupByList(list, pageSize)); }}