java8 内存分页工具 (记录一下)
package com.barton; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; /** * create by barton on 2020-6-30 */ public class MemPageHelper { public static <T> void pageHelper(Integer pageSize, List<T> list, Consumer<List<T>> consumer){ //默认 100 分页 if(pageSize == null || pageSize == 0){ pageSize = 100; } Integer totalSize = list.size(); Integer totalPage = totalSize / pageSize; if (totalSize % pageSize != 0) { totalPage += 1; if (totalSize < pageSize) { pageSize = totalSize; } } for (int pageNum = 1; pageNum < totalPage + 1; pageNum++) { int starNum = (pageNum - 1) * pageSize; int endNum = pageNum * pageSize > totalSize ? (totalSize) : pageNum * pageSize; consumer.accept(list.subList(starNum,endNum)); } } public static void main(String[] args) { List<Integer> list = new ArrayList<>(); for(int i=0;i<100;i++){ list.add(i); } pageHelper(25,list, pageList->{ System.out.println(pageList); } ); } }