java splits a large collection into small collections according to a fixed length

When we use the collections (Collection, List, Set, etc.) in java, we may need to split the large collection into N small collections according to a fixed length due to certain restrictions.

For example: There is a List<String> list with 1099 primitives. I need to split this collection into many small collections according to a fixed length of 50. The originality of each collection is 50 elements, and the length of the last small collection may not be the same. to 50.

How to do it? There is a lot of information on the Internet, the code is as follows:

 

    /**
     * Split collection
     *
     * @param <T> generic object
     * @param resList The collection that needs to be split
     * @param subListLength the number of elements in each subcollection
     * @return returns a list of split sets
     * The combination tool class of guava and common is used in the code
     **/
    public static <T> List<List<T>> split(List<T> resList, int subListLength) {
        if (CollectionUtils.isEmpty(resList) || subListLength <= 0) {
            return Lists.newArrayList();
        }
        List<List<T>> ret = Lists.newArrayList();
        int size = resList.size();
        if (size <= subListLength) {
            // The amount of data is less than the size specified by subListLength
            ret.add (resList);
        } else {
            int pre = size / subListLength;
            int last = size % subListLength;
            // The previous pre collections, each size is subListLength elements
            for (int i = 0; i < pre; i++) {
                List<T> itemList = Lists.newArrayList();
                for (int j = 0; j < subListLength; j++) {
                    itemList.add(resList.get(i * subListLength + j));
                }
                ret.add(itemList);
            }
            // last processing
            if (last > 0) {
                List<T> itemList = Lists.newArrayList();
                for (int i = 0; i < last; i++) {
                    itemList.add(resList.get(pre * subListLength + i));
                }
                ret.add(itemList);
            }
        }
        return ret;
    }

// run the code
public static void main(String[] args) {
    List<String> list = Lists.newArrayList();
    int size = 1099;
    for (int i = 0; i < size; i++) {
        list.add("hello-" + i);
    }

    List<List<String>> temps = split(list, 6);
    int j = 0;
    for (List<String> obj : temps) {
        System.out.println(String.format("row:%s -> size:%s,data:%s", ++j, obj.size(), obj));
    }
}

  In fact, I use more of the guava framework on weekdays, which comes with a method to implement the above set cutting function. code show as below:

 

public static void main(String[] args) {
    List<String> list = Lists.newArrayList();
    int size = 1099;
    for (int i = 0; i < size; i++) {
        list.add("hello-" + i);
    }
    // Cut the large set to the specified length: 11
    List<List<String>> rsList = Lists.partition(list, 11);
    int i = 0;
    for (List<String> obj : rsList) {
        System.out.println(String.format("row:%s -> size:%s,data:%s", ++i, obj.size(), obj));
    }
}

 It is recommended that you use the functions of guava, which are strictly unit-tested code.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616225&siteId=291194637