Java将List按指定个数分组

/**
     * List按个数分组
     *
     * @param list
     * @param quantity
     * @return
     */
    public static <T> List<List<T>> groupListByQuantity(List<T> list, int quantity) {
        List<List<T>> wrapList = new ArrayList<>();
        if (list == null || list.size() == 0) {
            return wrapList;
        }
        int count = 0;
        while (count < list.size()) {
            wrapList.add(list.subList(count, (count + quantity) > list.size() ? list.size() : (count + quantity)));
            count += quantity;
        }
        return wrapList;

    }

 

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页