Java Collection Split split(Collection set, int n)

Here you can find the source of split(Collection set, int n)

Description

Splits a collection in n new collections.

License

Open Source License

Parameter

Parameter Description
set the input collection
n the number of new collections
T the element type

Return

a list containing the new collections

Declaration

public static <T> List<T>[] split(Collection<T> set, int n) 

Method Source Code


//package com.java2s;
/* *********************************************************************** *
 * project: org.matsim.*/*ww  w .  j  av  a 2  s .  c  o  m*/
 * CollectionUtils.java
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 * copyright       : (C) 2011 by the members listed in the COPYING,        *
 *                   LICENSE and WARRANTY file.                            *
 * email           : info at matsim dot org                                *
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *   See also COPYING, LICENSE and WARRANTY file                           *
 *                                                                         *
 * *********************************************************************** */

import java.util.*;

public class Main {
    /**
     * Splits a collection in <tt>n</tt> new collections. The last collection may contain more elements than the others
     * if {@code set.size() % n != 0}.
     *
     * @param set the input collection
     * @param n   the number of new collections
     * @param <T> the element type
     * @return a list containing the new collections
     */
    public static <T> List<T>[] split(Collection<T> set, int n) {
        if (set.size() >= n) {
            @SuppressWarnings("unchecked")
            List<T>[] arrays = new List[n];
            int minSegmentSize = (int) Math.floor(set.size() / (double) n);

            int start = 0;
            int stop = minSegmentSize;

            Iterator<T> it = set.iterator();

            for (int i = 0; i < n - 1; i++) {
                int segmentSize = stop - start;
                List<T> segment = new ArrayList<T>(segmentSize);
                for (int k = 0; k < segmentSize; k++) {
                    segment.add(it.next());
                }
                arrays[i] = segment;
                start = stop;
                stop += segmentSize;
            }

            int segmentSize = set.size() - start;
            List<T> segment = new ArrayList<T>(segmentSize);
            for (int k = 0; k < segmentSize; k++) {
                segment.add(it.next());
            }
            arrays[n - 1] = segment;

            return arrays;
        } else {
            throw new IllegalArgumentException("n must not be smaller set size!");
        }
    }
}

Related

  1. getStringCollection(String str, String split)
  2. getSymbolSplitString(Collection collection, String symbol)
  3. split(Collection d, int n)
  4. split(Collection collections, String separator)
  5. split(Collection orig, int batchSize)
  6. split(final Collection collection)
  7. splitAndKeepEscapedSpaces(String string, boolean preserveEscapes, Collection into)
  8. splitToCollection(Collection collection, String valueString)
  9. splitToCollection(String list, String separator, Collection dest)