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 List<?> lastSubList(List<?> origin, int count) {
    if (origin == null || count <= 0) {
        return origin;
    }/*from  w w  w.j a v a  2s  .c o m*/
    if (origin.size() < count) {
        count = origin.size();
    }
    return origin.subList(origin.size() - count, origin.size());
}

From source file:Main.java

public static <E> boolean isSublistOf(List<E> bigger, List<E> smaller) {
    if (smaller.size() > bigger.size())
        return false;
    for (int start = 0; start + smaller.size() <= bigger.size(); ++start) {
        List<E> sublist = bigger.subList(start, start + smaller.size());
        if (sublist.equals(bigger)) {
            return true;
        }//from   w  ww  .  java 2s  .com
    }
    return false;
}

From source file:Main.java

/**
 * Does a list start with a sub-list. If either list is <code>null</code>, returns
 * <code>false</code>.//w ww  .  j a  v  a 2  s.  c o m
 *
 * @param <T>
 * @param list
 *            a list
 * @param subList
 *            sub-list
 * @return <code>true</code> if and only if list starts with subList
 */
public static <T> boolean startsWith(final List<? extends T> list, final List<? extends T> subList) {
    if ((list == null) || (subList == null) || (list.size() < subList.size())) {
        return false;
    }
    return list.subList(0, subList.size()).equals(subList);
}

From source file:Main.java

public static List<?> subList(List<?> origin, int count) {
    if (origin == null || count <= 0) {
        return origin;
    }//from ww  w  . ja  v  a 2s .  com
    if (origin.size() < count) {
        count = origin.size();
    }
    return origin.subList(0, count);
}

From source file:Main.java

public static <K, V> List<V> selectRandom(Map<K, V> map, int size) {
    if (isNull(map)) {
        return null;
    }/*  w  w w . ja v a2 s .c  o  m*/
    List<V> values = valueToList(map);
    if (values.size() <= size) {
        return new ArrayList<V>(values);
    }
    Collections.shuffle(values);
    return values.subList(0, size);
}

From source file:Main.java

public static <E> boolean isSublistOf(List<E> bigger, List<E> smaller) {
    if (smaller.size() > bigger.size()) {
        return false;
    }/*  ww w. j  av  a2  s. c om*/
    for (int start = 0; start + smaller.size() <= bigger.size(); ++start) {
        List<E> sublist = bigger.subList(start, start + smaller.size());
        if (sublist.equals(bigger)) {
            return true;
        }
    }
    return false;
}

From source file:org.javelin.sws.ext.utils.NamespaceUtils.java

/**
 * Converts package name to URL for namespace according to JAXB/JAX-WS conventions with separate domain and path fragments
 * /*from  ww w .  jav a  2 s .  c o  m*/
 * @param pkg
 * @param domainComponentCount number of package components to be converted into domain part of URL. If zero than entire package will be a domain
 * @return
 */
public static String packageNameToNamespace(Package pkg, int domainComponentCount) {
    Assert.notNull(pkg, "Package should not be null");
    Assert.isTrue(domainComponentCount != 1,
            "The domain part should not consist of one component. It may be zero or more than 1.");

    List<String> elements = new ArrayList<String>(Arrays.asList(pkg.getName().split("\\.")));
    if (domainComponentCount > 0) {
        List<String> domain = elements.subList(0, domainComponentCount);
        List<String> path = elements.subList(domainComponentCount, elements.size());
        Collections.reverse(domain);
        return "http://" + StringUtils.collectionToDelimitedString(domain, ".") + "/"
                + StringUtils.collectionToDelimitedString(path, "/");
    } else {
        Collections.reverse(elements);
        return "http://" + StringUtils.collectionToDelimitedString(elements, ".") + "/";
    }
}

From source file:Main.java

public static <T> List<T> takeLast(List<T> list, int length) {
    if (list.size() > length) {
        int fromIndex = Math.max(0, list.size() - length);
        int toIndex = list.size();
        List<T> newList = new ArrayList<T>();
        newList.addAll(list.subList(fromIndex, toIndex));
        return newList;
    }/*from www .j  a v  a 2s  . c  om*/
    return list;
}

From source file:Main.java

public static <T> List range(List<? extends T> list, T min, T max) {
    List<? super T> copyList = new ArrayList<>(list);
    Collections.sort(copyList, (o1, o2) -> Integer.compare(o1.hashCode(), o2.hashCode()));
    if (copyList.indexOf(min) < copyList.indexOf(max)) {
        return copyList.subList(copyList.indexOf(min), copyList.indexOf(max));
    } else {//from   w w w.j  a  v a  2s .  c  om
        return newArrayList();
    }
}

From source file:co.marcin.novaguilds.manager.PlayerManager.java

public static <T> List<T> limitList(List<T> list, int limit) {
    return list.subList(0, list.size() < limit ? list.size() : limit);
}