Java List Sub List subList(final List list, final int offset, final int amount)

Here you can find the source of subList(final List list, final int offset, final int amount)

Description

Returns a List containing the elements of the given List from offset and only the given amount.

License

Apache License

Parameter

Parameter Description
list the List to get a sublist from.
offset start of elements to return
amount amount of elements to return

Return

the sublist or null.

Declaration

public static <T> List<T> subList(final List<T> list, final int offset, final int amount) 

Method Source Code


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

import java.util.Collections;
import java.util.List;

public class Main {
    /**//from   w  w w . ja  v a2  s.  co m
     * Returns a {@link List} containing the elements of the given {@link List} from offset and only the given amount.
     * If there aren't enough elements in the given {@link List} for the requested amount the rest of the {@link List}
     * will returned. Returns null if the given {@link List} is null.
     *
     * @param list the {@link List} to get a sublist from.
     * @param offset start of elements to return
     * @param amount amount of elements to return
     * @return the sublist or null.
     */
    public static <T> List<T> subList(final List<T> list, final int offset, final int amount) {
        if (list == null) {
            return null;
        }

        if (offset >= list.size()) {
            return Collections.emptyList();
        }

        int toPos = offset + amount;
        if (toPos >= list.size()) {
            toPos = list.size();
        }

        return list.subList(offset, toPos);
    }
}

Related

  1. sub(List l, int index)
  2. subArray(List listA, List listB)
  3. subList(final Iterable

    parent, final Iterable child)

  4. subList(final List oriList, int[] indexes)
  5. subList(final List list, final int first, final int count)
  6. subList(final List list, final int startIndex, final int endIndex)
  7. subList(final List source, final int... indices)
  8. sublist(LinkedHashSet base, int start, int count)
  9. subList(List list, int start, int end)