The following code:
private static List<List<Object>> createBatch(List<Object> originalList, int batch_size) {
int Length = originalList.size();
int chunkSize = Length / batch_size;
int residual = Length-chunkSize*batch_size;
List<Integer> list_nums = new ArrayList<Integer>();
for (int i = 0; i < batch_size; i++) {
list_nums.add(chunkSize);
}
for (int i = 0; i < residual; i++) {
list_nums.set(i, list_nums.get(i) + 1);
}
List<Integer> list_index = new ArrayList<Integer>();
int cumulative = 0;
for (int i = 0; i < batch_size; i++) {
list_index.add(cumulative);
cumulative += list_nums.get(i);
}
list_index.add(cumulative);
List<List<Object>> listOfChunks = new ArrayList<List<Object>>();
for (int i = 0; i < batch_size; i++) {
listOfChunks.add(originalList.subList(list_index.get(i), list_index.get(i + 1)));
}
return listOfChunks;
}
produces the following output:
//[0,..,99] equally partition into 6 batch
// result:batch_size=[17,17,17,17,16,16]
//Continually partition into 6 batch, and residual also equally
//partition into top n batch
// Output:
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
[17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33]
[34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]
[51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67]
[68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83]
[84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99]