Example usage for java.util List subList

List of usage examples for java.util List subList

Introduction

In this page you can find the example usage for java.util List subList.

Prototype

List<E> subList(int fromIndex, int toIndex);

Source Link

Document

Returns a view of the portion of this list between the specified fromIndex , inclusive, and toIndex , exclusive.

Usage

From source file:Main.java

public static <V> List<V> shuffleAndSubList(List<V> list, int subSize) {
    if (list == null || list.size() == 0) {
        return null;
    }// w ww  .j  a v  a2  s.  com
    Collections.shuffle(list);
    list = list.size() > subSize ? list.subList(0, subSize) : list;
    return list;
}

From source file:Main.java

public static void removeTail(final List<?> list, final int newSize) {
    final int listSize = list.size();
    if (newSize < 0 || newSize >= listSize) {
        return;//from   w  w  w.  j a va 2 s.  co  m
    }
    list.subList(newSize, listSize).clear();
}

From source file:com.centurylink.cloud.sdk.core.services.filter.Filters.java

static <T extends Filter<T>> T reduce(List<T> filters, BinaryOperator<T> operator) {
    int length = filters.size();
    T head = filters.get(0);/*ww  w . j  a va 2 s .co  m*/
    List<T> tail = filters.subList(1, length);

    if (length == 1) {
        return head;
    } else if (length == 2) {
        return operator.apply(head, tail.get(0));
    } else {
        return operator.apply(head, reduce(tail, operator));
    }
}

From source file:Main.java

public static <T> List<T> subListOn(List<T> list, Predicate<T> predicate) {
    Optional<T> first = list.stream().filter(predicate).findFirst();
    if (!first.isPresent())
        return list;
    return list.subList(0, list.indexOf(first.get()));
}

From source file:Main.java

public static <T> List range(List<? extends T> list, T min, T max, Comparator<? super T> comparator) {
    List<? super T> copyList = new ArrayList<T>(list);
    Collections.sort(list, comparator);
    if (copyList.indexOf(min) < copyList.indexOf(max)) {
        return copyList.subList(copyList.indexOf(min), copyList.indexOf(max));
    } else {//  www  . ja  v  a 2s.c o  m
        return newArrayList();
    }
}

From source file:Main.java

/**
 * Split {@link List} at a index./*from  ww w .  j a va  2 s .  c  o  m*/
 *
 * @param <T> T
 * @param list {@link List}
 * @param index int
 * @return {@link List} that has either 1 or 2 elements
 */
public static <T> List<List<T>> split(final List<T> list, final int index) {

    List<List<T>> result = new ArrayList<>();

    if (index > 0 && index < list.size()) {
        result.add(list.subList(0, index));
        result.add(list.subList(index, list.size()));
    } else {
        result.add(list);
    }

    return result;
}

From source file:Main.java

private static <T> void permute(Map<String, T> basePermutation, List<String> remainingKeys,
        Map<String, List<T>> parameterValues, List<Map<String, T>> returnList) {

    String thisKey = remainingKeys.get(0);
    remainingKeys = remainingKeys.subList(1, remainingKeys.size());
    for (T value : parameterValues.get(thisKey)) {
        Map<String, T> permutation = new HashMap<String, T>(basePermutation);

        permutation.put(thisKey, value);

        if (remainingKeys.size() == 0) {
            returnList.add(permutation);
        } else {/*from  w  w  w.  ja v a  2s .  c o  m*/
            permute(permutation, remainingKeys, parameterValues, returnList);
        }
    }
}

From source file:Main.java

public static <V> void removeDuplicates(List<V> lst) {
    for (ListIterator<V> it = lst.listIterator(); it.hasNext();) {
        int idx = it.nextIndex();
        V elem = it.next();//w ww .java2 s .  c  o  m
        if (idx < lst.size() - 1 && lst.subList(idx + 1, lst.size() - 1).contains(elem))
            it.remove();
    }
}

From source file:Main.java

public static <T> String concat(List<T> col, int fromIndex, int toIndex, String separator) {

    if (fromIndex < 0 || toIndex > col.size() || fromIndex > toIndex)
        return "";

    return concat(col.subList(fromIndex + 1, toIndex), separator);

}

From source file:Main.java

/**
 * Converts a list to a new copy of array based on the start index and end index.
 * /*from  www  .  j  a  v a  2 s .c  o  m*/
 * @param list
 * @param startIndex
 * @param endIndex
 * @return
 */
@SuppressWarnings({ "unchecked", "cast" })
public static final <T> T[] toArray(List<T> list, int startIndex, int endIndex) {
    if (isEmpty(list)) {
        return (T[]) list.toArray();
    }
    List<T> subList = list.subList(startIndex, endIndex);
    return (T[]) subList
            .toArray((T[]) java.lang.reflect.Array.newInstance(list.get(0).getClass(), subList.size()));
}