Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static <T> Collection<Collection<T>> split(Collection<T> coll, int batchSize) {
        Collection<Collection<T>> batches = new ArrayList<>(batchSize);
        int batchCount = coll.size() / batchSize;
        for (int i = 0; i < batchCount; i++) {
            batches.add(new ArrayList<T>());
        }

        int index = 0;

        for (T t : coll) {
            ((ArrayList<Collection<T>>) batches).get(index).add(t);
            index = (index + 1) % batchCount;
        }

        return batches;
    }
}