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:de.fhg.iais.asc.sipmaker.SipMakerKey.java

private static SipMakerKey from(String[] splitted) {
    List<String> list = Arrays.asList(splitted);
    return fromTrunk(splitted[0], list.subList(1, list.size()));
}

From source file:Main.java

public static <E> boolean containsSequence(List<E> list, List<E> sequence) {
    for (int i = 0; i < list.size() && i + sequence.size() <= list.size(); i++) {
        List<E> sublist = list.subList(i, i + sequence.size());
        if (sublist.equals(sequence)) {
            return true;
        }//  w w  w  .  j  a v  a  2 s .c  o  m
    }
    return false;
}

From source file:Main.java

public static <O> List<O> safeSubList(List<O> l, int size) {
    try {//from w  ww  . java2s .  co  m
        if (l.size() < size) {
            return l;
        }

        return l.subList(0, size);
    } catch (Throwable ex) {
        return l;
    }
}

From source file:Main.java

public static <T> void removeWithoutUsingRemoveMethod(List<T> list, int index) {
    if (index < 0 || index >= list.size()) {
        throw new IllegalArgumentException("index out of range");
    }/* w w w  .ja  v a2 s. c  o  m*/
    List<T> part1 = new ArrayList<T>(list.subList(0, index));
    List<T> part2 = new ArrayList<T>(list.subList(index + 1, list.size()));
    list.clear();
    list.addAll(part1);
    list.addAll(part2);
}

From source file:Main.java

public static <T> List<? super T> limit(List<? super T> source, int size) {
    if ((source.size() == size) || (source.size() < size)) {
        return source;
    } else {/*from   www . j a v  a  2 s .co  m*/
        return source.subList(0, size);
    }
}

From source file:Main.java

public static <O> List<List<O>> split(List<O> l, int targetSize) {
    List<List<O>> groups = new ArrayList<List<O>>();
    for (int i = 0; i < l.size(); i += targetSize) {
        groups.add(l.subList(i, Math.min(i + targetSize, l.size())));
    }/*from  w  w w.  ja v a2  s.  co  m*/
    return groups;
}

From source file:Main.java

/**
 * Gets a sublist of a list, truncated at the end of the list if too many elements are selected.
 * This behaves exactly like List.subList, including all notes in its Javadoc concerning
 * structural modification of the backing List, etc. with one difference: if the end index is
 * beyond the end of the list, instead of throwing an exception, the sublist simply stops at the
 * end of the list.  After the fifth or so time writing this idiom, it seems worth having a
 * function for. :-)//w  ww .j  av a  2  s  .c  o m
 */
public static <T> List<T> truncatedSubList(List<T> inList, int start, int end) {
    // List.sublist will do our error checking for us
    final int limit = Math.min(end, inList.size());
    return inList.subList(start, limit);
}

From source file:Main.java

/**
 * Auxilliary method required for merging two consecutive sorted lists in
 * place.//from  w  w  w. j  a v a2s.c om
 *
 * <p>This implementation is based on:</p>
 *
 * <p>J. Chen, "<a href="http://dx.doi.org/10.1016/j.ipl.2005.11.018">A
 * simple algorithm for in-place merging</a>", Information Processing
 * Letters 98:34-40, 2006.</p>.
 *
 * This method is a direct transcription of Fig. 5.
 */
private static <T extends Comparable<? super T>> void mergeBandY(List<T> A, int z, int y, int yn) {
    while (z < y && y <= yn) {
        int j = z + indexOfMin(A.subList(z, y));
        if (A.get(j).compareTo(A.get(y)) <= 0) {
            Collections.swap(A, z, j);
        } else {
            Collections.swap(A, z, y);
            y++;
        }
        z++;
    }
    if (z < y) {
        Collections.sort(A.subList(z, yn + 1));
    }
}

From source file:Main.java

public static List<Message> getMessagesWithPage(Folder folder, int pageNum, int pageSize)
        throws MessagingException {
    Message[] messages = folder.getMessages();
    Arrays.sort(messages, new Comparator<Message>() {

        public int compare(Message o1, Message o2) {
            Message m1 = (Message) o1;/*  w w  w  .j  a v a 2 s . c  o  m*/
            Message m2 = (Message) o2;
            if (m1.getMessageNumber() > m2.getMessageNumber()) {
                return -1;
            }
            return 1;
        }

    });
    List<Message> list = Arrays.asList(messages);
    int start = (pageNum - 1) * pageSize;
    int end = pageNum * pageSize;
    return list.subList(start, end);
}

From source file:Main.java

public static <T> List<T> getPart(List<T> list, T start, T finish) {
    int startIndex = list.indexOf(start);
    if (startIndex < 0)
        return Collections.<T>emptyList();

    List<T> subList = list.subList(startIndex + 1, list.size());
    int endIndex = subList.indexOf(finish);
    return endIndex <= 0 ? Collections.<T>emptyList() : subList.subList(0, endIndex);
}