项目中使用的List切分工具,分享出来.
public static List<List<Integer>> subPage(List<Integer> list, int pageSize) {
int totalCount = list.size();
int pageCount;
int m = totalCount % pageSize;
if (m > 0) {
pageCount = totalCount / pageSize + 1;
} else {
pageCount = totalCount / pageSize;
}
List<List<Integer>> totalList = new ArrayList<List<Integer>>();
for (int i = 1; i <= pageCount; i++) {
if (m == 0) {
List<Integer> subList = list.subList((i - 1) * pageSize, pageSize * (i));
totalList.add(subList);
} else {
if (i == pageCount) {
List<Integer> subList = list.subList((i - 1) * pageSize, totalCount);
totalList.add(subList.stream().collect(Collectors.toList()));
} else {
List<Integer> subList = list.subList((i - 1) * pageSize, pageSize * i);
totalList.add(subList.stream().collect(Collectors.toList()));
}
}
}
return totalList;
}
网友评论