java对集合进行分页

    private static <T> List<List<T>> sub02(List<T> sourceList, int pageSize) {
        int total = sourceList.size();
        List<List<T>> resultList = new ArrayList<>();
        //总页数
        int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        for (int i = 0; i <= totalPage; i++) {
            if (i == totalPage - 1) {
                int start = (totalPage - 1) * pageSize;
                List<T> lessSubList = sourceList.subList(start, total);
                resultList.add(lessSubList);
                break;
            }
            List<T> subList = sourceList.subList(i * pageSize, (i + 1) * pageSize);
            resultList.add(subList);
        }
        return resultList;
    }

 

posted on 2022-11-04 15:17  哈皮的玩偶  阅读(277)  评论(0编辑  收藏  举报