Java tutorial
//package com.java2s; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; public class Main { /** * Split the list and return the stream with the resultant lists. * @param list to be split * @param size of each list after splitting. * @param <T> type of list. * @return {@link List} of {@link ArrayList}s */ public static <T> List<List<T>> splitArrayList(final List<T> list, final int size) { return splitListStream(list, size).map(ArrayList::new).collect(Collectors.toList()); } /** * Split the list and return the stream with the resultant lists. * * @param list to be split * @param size of each list after splitting. * @param <T> type of list. * @return {@link Stream} of {@link List}s */ public static <T> Stream<List<T>> splitListStream(final List<T> list, final int size) { if (size <= 0) { throw new IllegalArgumentException("Invalid Split Size"); } final int listSize = list.size(); if (listSize == 0) { return Stream.empty(); } return IntStream.rangeClosed(0, (listSize - 1) / size) .mapToObj(n -> list.subList(n * size, Math.min((n + 1) * size, listSize))); } }