Java List Split splitList(List _list, int _elements)

Here you can find the source of splitList(List _list, int _elements)

Description

Split a List into equal parts.

License

Open Source License

Parameter

Parameter Description
_list list to split
_elements elements per list
T type

Return

list of lists

Declaration

public static <T> List<List<T>> splitList(List<T> _list, int _elements) 

Method Source Code

//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;
    }
}

Related

  1. split(List lst)
  2. split(List toSplit, int howOften)
  3. split(List list, int divide)
  4. splitList(List files, int limit)
  5. splitList(List list, int number)
  6. splitList(List list, int page)
  7. splitList(List parent, int subSize)
  8. splitList(List source, int count)
  9. splitList(List sourceList, int spliTotal)