Here you can find the source of partitionIntoFolds( List
public static <T> List<Collection<T>> partitionIntoFolds( List<T> values, int numFolds)
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { /**/*w w w. ja v a 2s.c om*/ * Split a list into numFolds (roughly) equally sized folds. The earlier folds * may have one more item in them than later folds. */ public static <T> List<Collection<T>> partitionIntoFolds( List<T> values, int numFolds) { List<Collection<T>> folds = new ArrayList<Collection<T>>(); int numValues = values.size(); int foldSize = numValues / numFolds; int remainder = numValues % numFolds; int start = 0; int end = foldSize; for (int foldNum = 0; foldNum < numFolds; foldNum++) { // if we're in the first 'remainder' folds, we get an extra item if (foldNum < remainder) { end++; } folds.add(values.subList(start, end)); start = end; end += foldSize; } return folds; } }