Java Utililty Methods List Sub List

List of utility methods to do List Sub List

Description

The list of methods to do List Sub List are organized into topic(s).

Method

ArrayListcopySubList(final List list, final int fromIndex, final int toIndex)
Returns sub list with copied values.
return new ArrayList<T>(list.subList(fromIndex, toIndex));
ListcopySubList(List list, int fromIndex, int toIndex)
Unlike List.subList(), which returns a live view of a set of List elements, this method returns a new copy of the list.
if (fromIndex < 0)
    throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > list.size())
    throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
    throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
ArrayList<T> subList = new ArrayList<T>();
for (int i = fromIndex; i <= toIndex; i++) {
...
List>divideListInSublistsOfNSize(List list, int n)
divide List In Sublists Of N Size
final List<List<T>> container = new ArrayList<>();
List<T> cList = new ArrayList<>();
for (T o : list) {
    cList.add(o);
    if (cList.size() == n) {
        container.add(cList);
        cList = new ArrayList<>();
if (cList != null && cList.size() > 0) {
    container.add(cList);
return container;
int[]getIndexesId(List aList, List aSubList)
Returns an array of indexes for given list and given objects in list.
List<Integer> indexes = new ArrayList();
for (Object obj : aSubList) {
    int index = indexOfId(aList, obj);
    if (!indexes.contains(index))
        indexes.add(index);
int indxs[] = new int[indexes.size()];
for (int i = 0, iMax = indexes.size(); i < iMax; i++)
...
ListgetIndicesOfItems(List superList, List sublist)
get Indices Of Items
List<Integer> list = new ArrayList<Integer>(sublist.size());
for (Object object : sublist) {
    list.add(superList.indexOf(object));
return list;
ListgetRandomSubList(List inputList, double percentage)
get Random Sub List
if ((percentage < 0) || (percentage > 1)) {
    throw new IllegalArgumentException("percentage has to be between 0 and 1");
int numberOfElements = (int) (inputList.size() * percentage);
return getRandomSubList(inputList, numberOfElements);
ListgetSubList(int offset, int count, List originalList)
get Sub List
List<T> retVal = new ArrayList<T>(originalList);
truncateList(offset, count, retVal);
return retVal;
ListgetSubList(List entireList, Class cls)
get report elements
List list = new ArrayList();
for (Object e : entireList) {
    if (cls.isInstance(e))
        list.add(cls.cast(e));
return list;
ListgetSubList(List list, Class type)
Return all the classes in list which are of type.
return getSubList(list, type, INCLUDE);
ListgetSubList(List source, int nFrom, int nTo)
get Sub List
if (nFrom < 0) 
    nFrom = 0;
List list = new ArrayList();
for (int i = nFrom; i < (nTo + 1); ++i) {
    list.add(source.get(i));
return list;