分页方法定义: public static List<List<User>> splitList(List<User> list, int pageSize) {
int listSize = list.size(); int page = (listSize + (pageSize - 1)) / pageSize; List<List<User>>listArray = new ArrayList<List<User>>(); for (int i = 0; i<page; i++) { List subList = new ArrayList(); for (int j = 0; j<listSize; j++) { int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize; if ( pageIndex == (i + 1)) { subList.add(list.get(j)); } if ((j + 1) == ((j + 1) * pageSize)) { break; } } listArray.add(subList); } return listArray; }List<List<User>> usersize= splitList(userlist,3);
for (int i = 0; i < usersize.size(); i++) { List<User> userlists=usersize.get(i); System.out.println("序号"+i+"--"+userlists.toString());}
打印结果:
序号0--[User [uid=0, uname=lsm0], User [uid=1, uname=lsm1], User [uid=2uname=lsm2]]
序号1--[User [uid=3, uname=lsm3], User [uid=4, uname=lsm4], User [uid=5, uname=lsm5]] 序号2--[User [uid=6, uname=lsm6], User [uid=7, uname=lsm7], User [uid=8, uname=lsm8]] 序号3--[User [uid=9, uname=lsm9]]pageSize是定义每页存多少个 ,list是查询数据的集合
集合分子集合方法汇总:
public static <T> List<List<T>> avgAssignLst(List<T> source, int lstNum) {
List<List<T>> result = new ArrayList<List<T>>(lstNum + 1); int remaider = source.size() % lstNum; // (先计算出余数) int number = source.size() / lstNum; // 然后是商 int offset = 0;// 偏移量 List<T> subLst; for (int i = 0; i < lstNum; i++) { subLst = null; if (remaider > 0) { subLst = source.subList(i * number + offset, (i + 1) * number + offset + 1); remaider--; offset++; } else { subLst = source.subList(i * number + offset, (i + 1) * number + offset); } result.add(subLst); } return result; }