Java List Partition partition(List longList, int length)

Here you can find the source of partition(List longList, int length)

Description

partition

License

Apache License

Parameter

Parameter Description
longList a parameter
length a parameter

Declaration

public static <T> List<List<T>> partition(List<T> longList, int length) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**// w  w  w. j  av  a 2 s . c o  m
     * 
     * @param longList
     * @param length
     * @return
     */
    public static <T> List<List<T>> partition(List<T> longList, int length) {
        List<List<T>> result = new ArrayList<List<T>>();
        if (longList == null || longList.isEmpty())
            return result;
        if (longList.size() <= length) {
            result.add(longList);
        } else {
            int groups = (longList.size() - 1) / length + 1;
            for (int i = 0; i < groups - 1; i++) {
                result.add(new ArrayList(longList.subList(length * i, length * (i + 1))));
            }
            result.add(new ArrayList(longList.subList(length * (groups - 1), longList.size())));
        }
        return result;
    }
}

Related

  1. partition(List list, int num)
  2. partition(List list, int partitionSize)
  3. partition(List list, int size)
  4. partition(List list, int size)
  5. partition(List list, int size)
  6. partition(List orig, int size)
  7. partition(List sourceList, int numberOfSegments)
  8. partitionIntoFolds( List values, int numFolds)
  9. partitionList(final List originalList, final int chunkSize)