将List数据分成指定份数

public class ListUtil {

    public static void main(String[] args) {
//        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List<Integer> numbers = new ArrayList<>();

        List<List<Integer>> partition = partition(numbers, 3);
        partition.forEach(i->{
            System.out.println(i.toString());
        });


        List<Integer> integers = partition.get(2);
        System.out.println(integers.toString());
    }


    /**
     *
     * @Description 自定义List分片
     * @Author jianghuanxiang
     * @Date 2023/2/20 13:05
     * @param list:待分片数据
     * @param numSplits: 片数
     * @return: java.util.List<java.util.List<T>>
     **/
    public static <T> List<List<T>> partition(final List<T> list, final int numSplits) {
        if (list == null) {
            throw new NullPointerException("List must not be null");
        }
        if (numSplits <= 0) {
            throw new IllegalArgumentException("numSplits must be greater than 0");
        }
        return new ListUtil.Partition<>(list, numSplits);
    }

    private static class Partition<T> extends AbstractList<List<T>> {
        private final List<T> list;
        private final int size;


        private int getSize(final List<T> list, final int numSplits){
            int size = list.size();
            int chunkSize = (int) Math.ceil((double) size / numSplits);
            return chunkSize;
        }

        private Partition(final List<T> list, final int numSplits) {

            this.list = list;
            this.size = this.getSize(list,numSplits);
        }

        @Override
        public List<T> get(final int index) {
            final int listSize = size();
            if (index < 0) {
                throw new IndexOutOfBoundsException("Index " + index + " must not be negative");
            }
            if (index >= listSize) {
                throw new IndexOutOfBoundsException("Index " + index + " must be less than size " +
                        listSize);
            }
            final int start = index * size;
            final int end = Math.min(start + size, list.size());
            return list.subList(start, end);
        }

        @Override
        public int size() {
            return (int) Math.ceil((double) list.size() / (double) size);
        }

        @Override
        public boolean isEmpty() {
            return list.isEmpty();
        }
    }