Java List Partition splitListByPartitionSize(List list, int partitionSize)

Here you can find the source of splitListByPartitionSize(List list, int partitionSize)

Description

to split a list into lists with equally sized

License

Open Source License

Parameter

Parameter Description
list wanted to split
partitionSize size of every partition created from the list

Return

a List which holds ArrayLists with size of the given partitionSize

Declaration

public static List<List<?>> splitListByPartitionSize(List<?> list, int partitionSize) 

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  w  w  . j a v  a  2  s.  co m*/
     * to split a list into lists with equally sized
     * @param list wanted to split
     * @param partitionSize size of every partition created from the list
     * @return a List which holds ArrayLists with size of the given partitionSize
     */
    public static List<List<?>> splitListByPartitionSize(List<?> list, int partitionSize) {
        List<List<?>> partitionList = new ArrayList<>();
        for (int i = 0; i < list.size(); i += partitionSize) {
            List subList = new ArrayList(list.subList(i, Math.min(i + partitionSize, list.size())));
            partitionList.add(subList);
        }

        return partitionList;
    }
}

Related

  1. partitionList(final List originalList, final int chunkSize)
  2. partitionList(List list, int folds)
  3. partitionList(List list, final int partitionSize)
  4. partitionVariable(List arr, int chunk)
  5. removePartition(List donorPartitionList, int partition)