/**
* 自定义List分页工具
*/
public class PageUtil {
/**
* list分页
* @param list
* @param pageNo
* @param pageSize
* @param <T>
* @return
*/
public static <T> List<T> page(List<T> list, Integer pageSize,Integer pageNo) {
if (list == null || list.size() == 0) {
return new ArrayList<>();
}
// 记录总数
Integer count = list.size();
// 页数
Integer pageCount = 0;
if (count % pageSize == 0) {
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
// 开始索引
int fromIndex = 0;
// 结束索引
int toIndex = 0;
if (pageNo != pageCount) {
fromIndex = (pageNo - 1) * pageSize;
toIndex = fromIndex + pageSize;
} else {
fromIndex = (pageNo - 1) * pageSize;
toIndex = count;
}
try {
return list.subList(fromIndex, toIndex);
} catch (Exception e) {
return new ArrayList<>();
}
}
public static void main(String[] args) {
List<String> list = Lists.newArrayList();
for(int i=0;i<95;i++){
list.add(i+1+"");
}
Integer pageSize = 100;
Integer pageNo = 2;
List<String> pageList = page(list,pageSize,pageNo);
System.out.println(pageList);
}
}