Java List Partition partition(List sourceList, int numberOfSegments)

Here you can find the source of partition(List sourceList, int numberOfSegments)

Description

partition

License

Open Source License

Declaration

public static <T> List<List<T>> partition(List<T> sourceList, int numberOfSegments) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static <T> List<List<T>> partition(List<T> sourceList, int numberOfSegments) {
        List<List<T>> partitionedList = new ArrayList<>();
        if (sourceList.isEmpty()) {
            partitionedList.add(Collections.<T>emptyList());
            return partitionedList;
        }/* w w  w.  j a va2 s .com*/

        int actualNumberOfSegments = Math.min(sourceList.size(), numberOfSegments);

        for (int i = 0; i < actualNumberOfSegments; i++)
            partitionedList.add(new ArrayList<T>());

        int sourceElementIndex = 0;
        for (T sourceElement : sourceList) {
            int destinationListIndex = sourceElementIndex % actualNumberOfSegments;
            partitionedList.get(destinationListIndex).add(sourceElement);
            sourceElementIndex++;
        }

        return partitionedList;
    }
}

Related

  1. partition(List list, int size)
  2. partition(List list, int size)
  3. partition(List list, int size)
  4. partition(List longList, int length)
  5. partition(List orig, int size)
  6. partitionIntoFolds( List values, int numFolds)
  7. partitionList(final List originalList, final int chunkSize)
  8. partitionList(List list, int folds)
  9. partitionList(List list, final int partitionSize)