内存分页-查询条件长list时的分批查询
程序员文章站
2022-05-26 14:02:36
...
内存分页代码实现
// pageSize
public static final int PAGE_SIZE = 1000;
public static List<Result> getResultListPaged((List<T> totalList){
//totalPage
int totalPage = totalList .size() / PAGE_SIZE + (totalList .size() % PAGE_SIZE == 0 ? 0 : 1);
List<Result> resultList= new ArrayList<>(totalList.size());
for (int page = 1; page <= totalPage; page++) {
List<T> subList= getListByPage(totalList, page, PAGE_SIZE);
if (!CollectionUtils.isEmpty(subList)) {
List<ResultList> subResultList= resultService.getResultList(subList);
resultList.addAll(subResultList);
}
}
return resultList;
}
//分页方法
public static <T> List<T> getListByPage(List<T> totalList , int currentPage, int pageSize) {
int startIndex = (currentPage - 1) * pageSize;
if (startIndex > totalList .size()) {
return Lists.newArrayList();
}
int endIndex = startIndex + pageSize;
if (endIndex > totalList .size()) {
endIndex = totalList .size();
}
return resultList.subList(startIndex, endIndex);
}
`
推荐阅读