SplitUtil - 大集合List<Object>数据分割成Map<Integer,List<Object>>


/**
 * @author: luce
 * @Date: 2023/4/20 14:08
 * @Description:
 */
public class SplitUtil {

    /**
     * 数据分割方法
     * @param num 每页数据量
     * @param dataList 目标集合
     * @param 
     * @return 
     */
    public static <T> Map<Integer, List<T>> splitData(Integer num, List<T> dataList) {
        HashMap<Integer, List<T>> map = Maps.newHashMap();
        int size = dataList.size();
        //分割总页数
        int count = SplitUtil.countNumBydivisor(size, num);
        for (int i = 0; i < count; i++) {
            int lastNum = num * (i + 1);
            LinkedList<T> linkedList = new LinkedList<>();
            for (int j = num * i; j < lastNum && j < size; j++) {
                linkedList.add(dataList.get(j));
            }
            map.put(i, linkedList);
        }
        return map;
    }


    /**
     * @Description:按除数分割
     * @params: [size , divisor]
     * @return: int
     * @Author: luce
     * @Date: 2018/12/23 20:56
     **/
    public static int countNumBydivisor(int size, int divisor){

        int num = size % divisor;
        if (num > 0) {
            num = size / divisor + 1;
        } else {
            num = size / divisor;
        }
        return num ;
    }
}

你可能感兴趣的:(java)