Java List Partition partition(List list, Integer batchSize)

Here you can find the source of partition(List list, Integer batchSize)

Description

Partition a list in sub-lists, with max size equal to 'batchSize' parameter.

License

Open Source License

Parameter

Parameter Description
list The list to be partitioned.
batchSize The batch size, representing the maximum size allowed for a sub-list.

Declaration

public static <E> List<List<E>> partition(List<E> list, Integer batchSize) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Main {
    /**/* ww  w . j  a va2  s  .  c  o m*/
     * Partition a list in sub-lists, with max size equal to 'batchSize' parameter.
     * 
     * @param list
     *          The list to be partitioned.
     * @param batchSize
     *          The batch size, representing the maximum size allowed for a sub-list.
     * @return
     */
    public static <E> List<List<E>> partition(List<E> list, Integer batchSize) {
        List<List<E>> retObj = new ArrayList<List<E>>();

        if (isNotEmpty(list)) {
            retObj = new ArrayList<List<E>>();
            List<E> currentList = null;

            for (int i = 0; i < list.size(); i++) {
                if (i % batchSize == 0) {
                    currentList = new ArrayList<E>();
                    retObj.add(currentList);
                }
                currentList.add(list.get(i));
            }
        }
        return retObj;
    }

    /**
     * Check if set isn't empty.
     * 
     * @param set
     *          The set to be tested.
     * @return <code>true</code> is not empty, <code>false</code> otherwise.
     */
    public static <E> boolean isNotEmpty(Set<E> set) {
        return set != null && !set.isEmpty();
    }

    /**
     * This method checks if the list is not null and have elements.
     * 
     * @param <E>
     * 
     * @param list
     *          the list to check
     * @return true/false
     */
    public static <E> boolean isNotEmpty(List<E> list) {
        return list != null && !list.isEmpty();
    }

    /**
     * This method checks if the list is not null and have elements.
     * 
     * @param <E>
     * 
     * @param list
     *          the list to check
     * @return true/false
     */
    public static <E> boolean isNotEmpty(E[] list) {
        return list != null && list.length > 0;
    }

    /**
     * Check if set is empty.
     * 
     * @param set
     * @return
     */
    public static <E> boolean isEmpty(Set<E> set) {
        return !isNotEmpty(set);
    }

    public static <E> boolean isEmpty(List<E> list) {
        return !isNotEmpty(list);
    }

    public static <E> boolean isEmpty(E[] list) {
        return !isNotEmpty(list);
    }
}

Related

  1. partition(final List items, final int size)
  2. partition(final List list, final int length)
  3. partition(List all, int partitionSize)
  4. partition(List a, int lower, int upper)
  5. partition(List items, int slices)
  6. partition(List list, int num)