每一年都奔走在自己热爱里

没有人是一座孤岛,总有谁爱着你

Java处理List集合数据进行分页展示

1. PageInfo封装数据

1. 用封装好的PageInfo封装数据,返回的结果已经把总个数,每页数据,当前页,每页个数都封装好了(推荐使用):

2. 需要在POM文件导入依赖

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

3. 测试方法

  /**
     * 处理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;
    }

2. 循环截取某个页面的数据

  /**
     * 处理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;
    }

3. 完整测试类代码

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

}

3.1 测试结果展示

posted @ 2021-08-16 16:47  helloliyh  阅读(3960)  评论(0编辑  收藏  举报