有些需求需要计算,然后排名再进行分页,所以不好用mybatis的limit进行分页
/**
* 利用subList方法进行分页
* @param list 分页数据
* @param page 第几页
* @param size 显示条数
*/
public static <T>List<T> pageBySubList(List<T> list, int page,int size) {
int startIndex = getStartIndex(page,size);
int endIndex = getEndIndex(page,size);
int totalcount = list.size();
if (totalcount < startIndex) {
return null;
}
if (totalcount >= startIndex && totalcount <= endIndex ){
return list.subList(startIndex, totalcount);
}
if (totalcount >= startIndex && totalcount >= endIndex ){
return list.subList(startIndex, endIndex);
}
return null;
}
public static int getStartIndex(int page, int size) {
int pageTemp = 0;
if (page > 0) {
pageTemp = (page-1) * size;
}
return pageTemp;
}
public static int getEndIndex(int page, int size) {
if (page > 1) {
size = page * size;
}
return size;
}
JAVA程序subList分页
最新推荐文章于 2023-05-29 15:16:01 发布