Here you can find the source of splitList(List
Parameter | Description |
---|---|
_list | list to split |
_elements | elements per list |
T | type |
public static <T> List<List<T>> splitList(List<T> _list, int _elements)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { /**//from w ww . jav a 2s . c o m * Split a List into equal parts. * Last list could be shorter than _elements. * * @param _list list to split * @param _elements elements per list * @param <T> type * @return list of lists */ public static <T> List<List<T>> splitList(List<T> _list, int _elements) { List<List<T>> partitions = new ArrayList<>(); for (int i = 0; i < _list.size(); i += _elements) { partitions.add(_list.subList(i, Math.min(i + _elements, _list.size()))); } return partitions; } }