切分List工具

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

public static List> subPage(List 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> totalList = new ArrayList>();
        for (int i = 1; i <= pageCount; i++) {
            if (m == 0) {
                List subList = list.subList((i - 1) * pageSize, pageSize * (i));
                totalList.add(subList);
            } else {
                if (i == pageCount) {
                    List subList = list.subList((i - 1) * pageSize, totalCount);

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

        return totalList;
    }

你可能感兴趣的:(切分List工具)