Here you can find the source of partition(List
public static <T> List<List<T>> partition(List<T> sourceList, int numberOfSegments)
//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; } }