Java list 分页(多种方式) 方式一: public static void fenye(List list,int pagesize){ int totalcount=list.size(); int pagecou
方式一:
public static void fenye(List list,int pagesize){ int totalcount=list.size(); int pagecount=0; int m=totalcount%pagesize; if (m>0){ pagecount=totalcount/pagesize+1; }else{ pagecount=totalcount/pagesize; } for(int i=1;i<=pagecount;i++){ if (m==0){ List subList= list.subList((i-1)*pagesize,pagesize*(i)); System.out.println(subList); }else{ if (i==pagecount){ List subList= list.subList((i-1)*pagesize,totalcount); System.out.println(subList); }else{ List subList= list.subList((i-1)*pagesize,pagesize*(i)); System.out.println(subList); } } }}List list=new ArrayList();for(int i=1;i<52;i++){ list.add(i);}fenye(list,10);
方式二:
public List> splitList(List list, int pageSize) { int listSize = list.size(); int page = (listSize + (pageSize - 1)) / pageSize; List> listArray = new ArrayList>(); for (int i = 0; i < page; i++) { List subList = new ArrayList(); for (int j = 0; j < listSize; j++) { int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize; if (pageIndex == (i + 1)) { subList.add(list.get(j)); } if ((j + 1) == ((j + 1) * pageSize)) { break; } } listArray.add(subList); } return listArray; }
方式三:
public SearchResult paging(List list, int pageNumber, int pageSize) throws Exception{ List pageList = new ArrayList(); int currIdx = (pageNumber > 1 ? (pageNumber -1) * pageSize : 0); for (int i = 0; i < pageSize && i < list.size() - currIdx; i++){ T listNew = list.get(currIdx + i); pageList.add(listNew); } SearchResult searchResult = new SearchResult(); searchResult.setList(pageList); searchResult.setTotal(list.size()); if (searchResult.getTotal()%pageSize == 0){ searchResult.setPages((int)searchResult.getTotal()/pageSize); }else { searchResult.setPages((int)searchResult.getTotal()/pageSize + 1); } searchResult.setPageNum(pageNumber); searchResult.setPageSize(pageSize); searchResult.setSize(pageList.size()); //是否是第一页 if (pageNumber == 1){ searchResult.setIsFirstPage(true); }else { searchResult.setIsFirstPage(false); } //是否是最后一页 if ((searchResult.getTotal() - pageNumber*pageSize) > 0){ searchResult.setIsLastPage(false); }else { searchResult.setIsLastPage(true); } return searchResult; }
方式四:
List list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); int page = 2;//相当于pageNo-------分页中的pi int count = 20;//相当于pageSize-------分页中的ps int size = list.size(); int pageCount=size/count; int fromIndex = count * (page - 1); int toIndex = fromIndex + count; if (toIndex >= size) { toIndex = size; } if(page>pageCount+1){ fromIndex=0; toIndex=0; } System.out.println(list.subList(fromIndex, toIndex));
例子:
rckqMonthlyList为一个list if (rckqMonthlyList.size() <= 0) { map.put("totalPage", 1); map.put("pageIndex", 1); map.put("pageSize", query.getPs()); map.put("totalRow", 0); map.put("hasNext", false); map.put("list", rckqMonthlyList); return new ApiResult().success(map); } //有数据时 //计算总页数 Integer totalPage = rckqMonthlyList.size() / query.getPs();// if (rckqMonthlyList.size() % query.getPs() > 0) {// totalPage++;// } int fromIndex = query.getPs() * (query.getPi() - 1); int toIndex = fromIndex + query.getPs(); if (toIndex >= rckqMonthlyList.size()) { toIndex = rckqMonthlyList.size(); } if(query.getPi()>totalPage+1){ fromIndex=0; toIndex=0; } Boolean hasNext = query.getPi() != totalPage; map.put("totalPage", totalPage); map.put("pageIndex", query.getPi()); map.put("pageSize", query.getPs()); map.put("totalRow", rckqMonthlyList.size()); map.put("hasNext", hasNext); map.put("list", rckqMonthlyList.subList(fromIndex,toIndex)); return new apiResult().success(map);
来源地址:https://blog.csdn.net/fangzhen1111/article/details/131109297
--结束END--
本文标题: Java list 分页(多种方式)
本文链接: https://lsjlt.com/news/400517.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0