Java處理List集合數據進行分頁展示


第一種方法是用封裝好的PageInfo封裝數據,返回的結果已經把總個數,每頁數據,當前頁,每頁個數都封裝好了(推薦使用):

需要在POM文件導入依賴

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.0.1</version>
</dependency>

 

  /** * 處理List集合數據進行分頁 * * @param currentPage 當前頁 * @param pageSize 每頁數據個數 * @param list 進行分頁的數據 * @param <T> * @return
     */
    public static <T> PageInfo<T> queryPageInfo(int currentPage, int pageSize, List<T> list) { int total = list.size(); if (total > pageSize) { int toIndex = pageSize * currentPage; if (toIndex > total) { toIndex = total; } 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); PageInfo<T> pageInfo = new PageInfo<>(page); return pageInfo; }

 

第二種方法是循環截取某個頁面的數據:

  /** * 處理List集合數據進行分頁 * * @param currentPage 當前頁 * @param pageSize 每頁數據個數 * @param list 進行分頁的數據 * @param <T> * @return
     */
    public static <T> List<T> getPageInfo(int currentPage, int pageSize, List<T> list) { List<T> newList = new ArrayList<>(); if (list != null && list.size() > 0) { int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0); for (int i = 0; i < pageSize && i < list.size() - currIdx; i++) { newList.add(list.get(currIdx + i)); } } return newList; }

 

完整測試類代碼:

import java.util.ArrayList; import java.util.List; import com.github.pagehelper.Page; import com.github.pagehelper.PageInfo; public class TestListPage { public static void main(String[] args) { List<String> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { list.add(i + 1 + ""); } int currentPage = 1; int pageSize = 3; List<String> pageInfo = getPageInfo(currentPage, pageSize, list); System.out.println("總條數:" + list.size()); System.out.print("數據:"); pageInfo.forEach(e -> System.out.print(e)); System.out.println(); System.out.println("當前頁:" + currentPage); System.out.println("每頁個數:" + pageSize); System.out.println("總頁數:" + ((list.size() / pageSize) + 1)); System.out.println("==========================="); PageInfo<String> queryPageInfo = queryPageInfo(currentPage, pageSize, list); System.out.println("總條數:" + queryPageInfo.getTotal()); System.out.print("數據:"); queryPageInfo.getList().forEach(e -> System.out.print(e)); System.out.println(); System.out.println("當前頁:" + queryPageInfo.getPageNum()); System.out.println("每頁個數:" + queryPageInfo.getPageSize()); System.out.println("總頁數:" + queryPageInfo.getPages()); } /** * 處理List集合數據進行分頁 * * @param currentPage 當前頁 * @param pageSize 每頁數據個數 * @param list 進行分頁的數據 * @param <T> * @return
     */
    public static <T> List<T> getPageInfo(int currentPage, int pageSize, List<T> list) { List<T> newList = new ArrayList<>(); if (list != null && list.size() > 0) { int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0); for (int i = 0; i < pageSize && i < list.size() - currIdx; i++) { newList.add(list.get(currIdx + i)); } } return newList; } /** * 處理List集合數據進行分頁 * * @param currentPage 當前頁 * @param pageSize 每頁數據個數 * @param list 進行分頁的數據 * @param <T> * @return
     */
    public static <T> PageInfo<T> queryPageInfo(int currentPage, int pageSize, List<T> list) { int total = list.size(); if (total > pageSize) { int toIndex = pageSize * currentPage; if (toIndex > total) { toIndex = total; } 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); PageInfo<T> pageInfo = new PageInfo<>(page); return pageInfo; } }

 

測試結果展示:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM