List 内存 分页

package utils;


import java.util.ArrayList;
import java.util.List;


/**
 * @author NULL
 */
public class ListPageTools {


//1.1: 根据页面大小 分割list
public static <T> List<List<T>> splitListOfPageSize(List<T> list, int pageSize) {


List<List<T>> listArray = new ArrayList<List<T>>();// 创建list数组,用来保存分割后的list


if (list!=null && list.size()>0 && pageSize > 0) {


int page = (list.size() + (pageSize - 1)) / pageSize;// 总页数


for (int i = 0; i < page; i++) {
List<T> partList = new ArrayList<T>();
partList = list.subList(i * pageSize, (i + 1) * pageSize > list.size() ? list.size() : (i + 1) * pageSize);
listArray.add(partList);
}
}


return listArray;
}


//1.2: 根据页面数量 分割list
public static <T> List<List<T>> splitListOfPageCount(List<T> list, int pageCount) {


List<List<T>> listArray = new ArrayList<List<T>>();// 创建list数组,用来保存分割后的list

if (list!=null && list.size()>0 && pageCount > 0) {
if (pageCount > list.size()){//假如页面数量大于list数量,则按list数量分割
pageCount = list.size();
}
int page = (list.size() + (pageCount - 1)) / pageCount;// 每页大小
if (page * (pageCount - 1) > list.size()) {
page--;
}
for (int i = 0; i < pageCount; i++) {
List<T> partList = new ArrayList<T>();
if (i * page >= list.size()) {
break;
}
partList = list.subList(i * page, i + 1 == pageCount ? list.size() : (i + 1) * page > list.size() ? list.size() : (i + 1) * page);
listArray.add(partList);
}
}


return listArray;
}

//2.1: 将list分页,按照页面大小,页面从0开始
public static <T> List<T> splitListOfPageSizeAndIndex(List<T> list, int pageSize,int pageIndex) {

List<T> result = null;
if(list!=null && list.size()>0){
List<List<T>> splitListOfPageSize = ListPageTools.splitListOfPageSize(list,pageSize);

if(splitListOfPageSize!=null && splitListOfPageSize.size()>0 && splitListOfPageSize.size()>=pageIndex){
if(pageIndex>=0) {
result = splitListOfPageSize.get(pageIndex);
}
}
}

return result;
}

//2.2: 将list分页,按照页面数量,页码从0开始
public static <T> List<T> splitListOfPageCountAndIndex(List<T> list, int pageCount, int pageIndex) {

List<T> result = null;
if(list!=null && list.size()>0){
List<List<T>> splitListOfPageCount = ListPageTools.splitListOfPageSize(list,pageCount);

if(splitListOfPageCount!=null && splitListOfPageCount.size()>0 && splitListOfPageCount.size()>=pageIndex){
if(pageIndex>=0) {
result = splitListOfPageCount.get(pageIndex);
}
}
}

return result;
}


}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页