简介:
基于一些特殊场景,无法使用 SQL ,但需要完成类似分页功能。做了一个简单的基于 List 的分页
代码如下:
/**
* pageBySubList分页
* @param list 集合
* @param pagesize 每页条数
* @param currentPage 当前页
* @return
*/
public static List pageBySubList(List list, int pagesize, int currentPage) {
int totalcount = list.size();
int pagecount = 0;
List subList=new ArrayList<>();
int m = totalcount % pagesize;
if (m > 0) {
pagecount = totalcount / pagesize + 1;
} else {
pagecount = totalcount / pagesize;
}
if(((currentPage - 1) * pagesize )>=totalcount){
return subList;
}
if (m == 0) {
subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage)>totalcount?totalcount-1:pagesize * (currentPage));
} else {
if (currentPage == pagecount) {
subList = list.subList((currentPage - 1) * pagesize, totalcount);
} else {
subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage)>totalcount?totalcount-1:pagesize * (currentPage));
}
}
return subList;
}
此文章为该作者原创,禁止转载。