package com.sunwin.robot2ips.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author machao
 * @version 1.0
 * @description: TODO
 * @date 2021/7/5 11:01
 */
public class ListSplitUtil<T> {


    /**
     * list切割
     *
     * @param source 需要切割的list
     * @param n      切割的list的数量单位
     * @param <T>
     * @return
     */
    public <T> List<List<T>> fixedGrouping(List<T> source, int n) {
        if (null == source || source.size() == 0 || n <= 0) {
            return null;
        }
        List<List<T>> result = new ArrayList<>();
        int remainder = source.size() % n;
        int size = (source.size() / n);
        for (int i = 0; i < size; i++) {
            List<T> subset;
            subset = source.subList(i * n, (i + 1) * n);
            result.add(subset);
        }
        if (remainder > 0) {
            List<T> subset;
            subset = source.subList(size * n, size * n + remainder);
            result.add(subset);
        }
        return result;
    }


}
