Java List Partition partitionIntoFolds( List values, int numFolds)

Here you can find the source of partitionIntoFolds( List values, int numFolds)

Description

Split a list into numFolds (roughly) equally sized folds.

License

Open Source License

Declaration

public static <T> List<Collection<T>> partitionIntoFolds(
        List<T> values, int numFolds) 

Method Source Code

//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;
    }
}

Related

  1. partition(List list, int size)
  2. partition(List list, int size)
  3. partition(List longList, int length)
  4. partition(List orig, int size)
  5. partition(List sourceList, int numberOfSegments)
  6. partitionList(final List originalList, final int chunkSize)
  7. partitionList(List list, int folds)
  8. partitionList(List list, final int partitionSize)
  9. partitionVariable(List arr, int chunk)