Java Collection Split split(Collection orig, int batchSize)

Here you can find the source of split(Collection orig, int batchSize)

Description

split

License

Open Source License

Declaration

public static <T> List<List<T>> split(Collection<T> orig, int batchSize) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import java.util.List;

public class Main {

    public static <T> List<List<T>> split(Collection<T> orig, int batchSize) {
        if (orig == null || orig.isEmpty() || batchSize < 1) {
            return Collections.emptyList();
        }/* ww w .  jav  a 2s.  c o m*/
        int size = orig.size();
        int len = calSplitLen(batchSize, size);
        List<List<T>> result = new ArrayList<>(len);
        List<T> list = null;
        if (orig instanceof List) {
            list = (List<T>) orig;
        } else {
            list = new ArrayList<>(orig);
        }
        for (int i = 0; i < len; i++) {
            result.add(new ArrayList<>(
                    list.subList(i * batchSize, ((i + 1) * batchSize) < size ? (i + 1) * batchSize : size)));
        }
        return result;
    }

    private static int calSplitLen(int batchSize, int size) {
        return ((size - 1) / batchSize) + 1;
    }
}

Related

  1. getCollectionStringBySplit(Collection collection, String split)
  2. getStringCollection(String str, String split)
  3. getSymbolSplitString(Collection collection, String symbol)
  4. split(Collection d, int n)
  5. split(Collection collections, String separator)
  6. split(Collection set, int n)
  7. split(final Collection collection)
  8. splitAndKeepEscapedSpaces(String string, boolean preserveEscapes, Collection into)
  9. splitToCollection(Collection collection, String valueString)