Java List Sub List divideListInSublistsOfNSize(List list, int n)

Here you can find the source of divideListInSublistsOfNSize(List list, int n)

Description

divide List In Sublists Of N Size

License

Open Source License

Declaration

public static <T> List<List<T>> divideListInSublistsOfNSize(List<T> list, int n) 

Method Source Code

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

import java.util.*;

public class Main {
    public static <T> List<List<T>> divideListInSublistsOfNSize(List<T> list, int n) {
        final List<List<T>> container = new ArrayList<>();

        List<T> cList = new ArrayList<>();
        for (T o : list) {
            cList.add(o);//from  w w w .  j  a  v a  2 s .  c  o  m

            if (cList.size() == n) {
                container.add(cList);
                cList = new ArrayList<>();
            }
        }

        if (cList != null && cList.size() > 0) {
            container.add(cList);
        }

        return container;
    }
}

Related

  1. copySubList(final List list, final int fromIndex, final int toIndex)
  2. copySubList(List list, int fromIndex, int toIndex)
  3. getIndexesId(List aList, List aSubList)
  4. getIndicesOfItems(List superList, List sublist)
  5. getRandomSubList(List inputList, double percentage)
  6. getSubList(int offset, int count, List originalList)