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;
  }
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 ( foldNum < remainder ) { end ++ ; } folds . add ( values . subList ( start , end ) ) ; start = end ; end += foldSize ; } return folds ; }
Split a list into numFolds (roughly) equally sized folds. The earlier folds
may have one more item in them than later folds.