对集合List<Map<String,Object>>进行一个分页

需求要对集合List<Map<String,Object>>进行一个分页:
/**
     * 利用subList方法进行分页
     *
     * @param list        分页数据
     * @param pagesize    页面大小
     * @param currentPage 当前页面(从0开始计算)
     */

    public static List<Map<String,Object>> pageBySubList(List list, int pagesize, int currentPage) {
        List<Map<String,Object>> subList = new ArrayList<>();
        try {
            int totalcount = list.size();
            currentPage = currentPage + 1;
            int pagecount = 0;

            int m = totalcount % pagesize;

            if (m > 0) {

                pagecount = totalcount / pagesize + 1;

            } else {

                pagecount = totalcount / pagesize;

            }

            if (m == 0) {

                subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));

            } else {

                if (currentPage == pagecount) {

                    subList = list.subList((currentPage - 1) * pagesize, totalcount);

                } else {

                    subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage));

                }

            }

        } catch (Exception e) {

        }
        return subList;
    }

 

posted @ 2022-11-28 11:47  浮笙芸芸  阅读(288)  评论(0)    收藏  举报