All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.commons.utils.SplitConn Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
package com.github.commons.utils;

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

public class SplitConn {

    public static  List> splitList(List list, int n) {
        List> strList = new ArrayList<>();
        if (list == null) return strList;
        int size = list.size();
        int quotient = size / n; // 商数
        int remainder = size % n; // 余数
        int offset = 0; // 偏移量
        int len = quotient > 0 ? n : remainder; // 循环长度
        int start = 0;    // 起始下标
        int end = 0;    // 结束下标
        List tempList = null;
        for (int i = 0; i < len; i++) {
            if (remainder != 0) {
                remainder--;
                offset = 1;
            } else {
                offset = 0;
            }
            end = start + quotient + offset;
            tempList = list.subList(start, end);
            start = end;
            strList.add(tempList);
        }
        return strList;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy