Here you can find the source of splitListToSubLists(List
public static <T> List<List<T>> splitListToSubLists(List<T> parentList, int subListSize)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { public static <T> List<List<T>> splitListToSubLists(List<T> parentList, int subListSize) { List<List<T>> subLists = new ArrayList<List<T>>(); if (subListSize > parentList.size()) { subLists.add(parentList);/*www . j av a 2 s. c o m*/ } else { int remainingElements = parentList.size(); int startIndex = 0; int endIndex = subListSize; do { List<T> subList = parentList.subList(startIndex, endIndex); subLists.add(subList); startIndex = endIndex; if (remainingElements - subListSize >= subListSize) { endIndex = startIndex + subListSize; } else { endIndex = startIndex + remainingElements - subList.size(); } remainingElements -= subList.size(); } while (remainingElements > 0); } return subLists; } public static int size(Collection<?> col) { return col == null ? 0 : col.size(); } }
parent, final Iterable