1. 程式人生 > 其它 >Java處理List集合資料進行分頁展示

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;
    }

}

測試結果展示: