java对list<Object>进行手动分页实现
该方法使用的jdk1.6以后自带的分页工具类实现
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add("Luo");
list.add("Tian");
list.add("yan");
list.add("23");
list.add("elegant");
list.add("optimistic");
list.add("mysterious");
System.out.println("分页前数据:"+list);
//分页之后
PageInfo pageInfo = getPageInfo(2, 2, list);
System.out.println("总大小:"+pageInfo.getTotal());
System.out.println("当前页:"+pageInfo.getPageNum());
System.out.println("当前页大小:"+pageInfo.getPageSize());
System.out.println("当前页数据:"+pageInfo.getList());
}
public static <T> PageInfo<T> getPageInfo(int currentPage, int pageSize, List<T> list) {
int total = list.size();
if (total > pageSize) {
int toIndex = pageSize * currentPage;
if (toIndex > total) {
toIndex = total;
}
int totalPage = total % pageSize == 0 ? (total/pageSize) : (total/pageSize)+1;
if (totalPage < currentPage){
list = new ArrayList<>();
}else {
list = list.subList(pageSize * (currentPage - 1), toIndex);
}
}
Page<T> page = new Page<>(currentPage, pageSize);
page.addAll(list);
page.setPages((total + pageSize - 1) / pageSize);
page.setTotal(total);
return new PageInfo<>(page);
}
效果
到此这篇关于java对list<Object>进行手动分页实现的文章就介绍到这了,更多相关java list<Object>手动分页内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章