java专题Java学习笔记Java 杂谈

切分List工具

2018-02-28  本文已影响16人  H_Man

项目中使用的List切分工具,分享出来.

public static List<List<Integer>> subPage(List<Integer> list, int pageSize) {
        int totalCount = list.size();
        int pageCount;
        int m = totalCount % pageSize;

        if (m > 0) {
            pageCount = totalCount / pageSize + 1;
        } else {
            pageCount = totalCount / pageSize;
        }

        List<List<Integer>> totalList = new ArrayList<List<Integer>>();
        for (int i = 1; i <= pageCount; i++) {
            if (m == 0) {
                List<Integer> subList = list.subList((i - 1) * pageSize, pageSize * (i));
                totalList.add(subList);
            } else {
                if (i == pageCount) {
                    List<Integer> subList = list.subList((i - 1) * pageSize, totalCount);

                    totalList.add(subList.stream().collect(Collectors.toList()));
                } else {
                    List<Integer> subList = list.subList((i - 1) * pageSize, pageSize * i);
                    totalList.add(subList.stream().collect(Collectors.toList()));
                }
            }
        }

        return totalList;
    }
上一篇 下一篇

猜你喜欢

热点阅读