java List 等份截取

/**
     * 描述: 等份截取
     * @author eyesmooon
     * @param list
     * @param size
     * @return 
     * @date:2018年11月8日 下午8:00:36
     */
    public static <T> List<List<T>> subList(List<T> list, int size) {
        List<List<T>> lists = new ArrayList<List<T>>();
        if (list != null && size > 0) {
            int listSize = list.size();
            if (listSize <= size) {
                lists.add(list);
                return lists;
            }
            int batchSize = listSize / size;
            int remain = listSize % size;
            for (int i = 0; i < batchSize; i++) {
                int fromIndex = i * size;
                int toIndex = fromIndex + size;
                // System.out.println("fromIndex=" + fromIndex + ", toIndex=" +
                // toIndex);
                lists.add(list.subList(fromIndex, toIndex));
            }
            if (remain > 0) {
                // System.out.println("fromIndex=" + (listSize-remain) + ",
                lists.add(list.subList(listSize - remain, listSize));
            }
        }
        return lists;
    }

 

posted @ 2018-11-08 20:02  月眸  阅读(371)  评论(0编辑  收藏  举报