Example usage for java.util ListIterator next

List of usage examples for java.util ListIterator next

Introduction

In this page you can find the example usage for java.util ListIterator next.

Prototype

E next();

Source Link

Document

Returns the next element in the list and advances the cursor position.

Usage

From source file:Main.java

public final static <T> T peekPrevious(ListIterator<T> iterator) {
    T previous = iterator.previous();//from   w  ww  .  j  av  a  2  s  .c om
    iterator.next();
    return previous;
}

From source file:Main.java

public static <T> void symmetricSort(List<T> list) {
    @SuppressWarnings("unchecked")
    T[] array = (T[]) new Object[list.size()];
    for (int i = 0; i < list.size(); i++) {
        if (i % 2 == 0) {
            array[(list.size() - i) / 2 + (list.size() % 2 - 1)] = list.get(i);
        } else {/* w  ww  . j  a  v a 2s.co m*/
            array[(list.size() + i) / 2] = list.get(i);
        }
    }

    int j = 0;
    ListIterator<T> it = list.listIterator();
    while (it.hasNext()) {
        it.next();
        it.set(array[j++]);
    }
}

From source file:Main.java

public static <E> void swap(final List<E> list, final int i, final int j) {
    if (list instanceof RandomAccess) {
        list.set(i, list.set(j, list.get(i)));
    } else {//from ww w. j ava  2  s . co m
        final ListIterator<E> iterator = list.listIterator(i);
        iterator.set(list.set(j, iterator.next()));
    }
}

From source file:Main.java

/**
 * Keeps and returns the original list./*  w  w w . jav a  2 s .  c o m*/
 * 
 * @param <T>
 * @param list
 * @return removes any duplicates in the list. Keeps the first occurrence of duplicates.
 */
public static <T> List<T> removeDuplicates(List<T> list) {
    HashSet<T> set = new HashSet<T>(list.size());
    ListIterator<T> i = list.listIterator();
    while (i.hasNext()) {
        T cur = i.next();
        if (set.contains(cur)) {
            i.remove();
        } else {
            set.add(cur);
        }
    }

    return list;
}

From source file:Main.java

/**
 * Removed null elements in-place./*ww  w.  j a va2s. c  o m*/
 * 
 * @param <T>
 * @param col
 */
public static <T> void clearNulls(List<T> col) {
    ListIterator<T> iter = col.listIterator();
    while (iter.hasNext()) {
        if (iter.next() == null) {
            iter.remove();
        }
    }
}

From source file:Main.java

public static <E> int indexOf(List<E> c, E item, BiPredicate<? super E, ? super E> equalTester) {
    ListIterator<E> iter = c.listIterator();
    while (iter.hasNext()) {
        int index = iter.nextIndex();
        if (equalTester.test(iter.next(), item)) {
            return index;
        }/*from w w w .  java  2s. com*/
    }
    return -1;
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.fields.FieldUtils.java

public static List<Individual> removeIndividualsAlreadyInRange(List<Individual> individuals,
        List<ObjectPropertyStatement> stmts, String predicateUri, String objectUriBeingEdited) {
    HashSet<String> range = new HashSet<String>();

    for (ObjectPropertyStatement ops : stmts) {
        if (ops.getPropertyURI().equals(predicateUri))
            range.add(ops.getObjectURI());
    }//w w  w . j  av  a 2 s.  c  o m

    int removeCount = 0;
    ListIterator<Individual> it = individuals.listIterator();
    while (it.hasNext()) {
        Individual ind = it.next();
        if (range.contains(ind.getURI()) && !(ind.getURI().equals(objectUriBeingEdited))) {
            it.remove();
            ++removeCount;
        }
    }

    return individuals;
}

From source file:Main.java

private static <T> T findNext(ListIterator<T> iter) {
    T next = null;//from  w w w .  j  av  a 2  s.  c  o  m
    if (iter.hasNext()) {
        next = iter.next();
        iter.previous(); // come back
    }
    return next;
}

From source file:Main.java

public static long iterRemove(List list) {
    ListIterator iter = list.listIterator();
    long start, stop, result = 0;
    for (int i = 0; i < 100; i++) {
        iter.next();
        start = System.nanoTime();
        iter.remove();/*from   w ww. jav  a  2s  . co  m*/
        stop = System.nanoTime();
        result += stop - start;
    }
    return result / 100;
}

From source file:Main.java

/**
 *  Resizes the passed list to N entries. Will add the specified object if
 *  the list is smaller than the desired size, discard trailing entries if
 *  larger. This is primarily used to presize a list that will be accessed
 *  by index, but it may also be used to truncate a list for display.
 *
 *  @return The list, as a convenience for callers
 *
 *  @throws UnsupportedOperationException if the list does not implement
 *          <code>RandomAccess</code> and its list iterator does not
 *          support the <code>remove()</code> operation
 *//*  w  ww .  j  a v  a 2 s  . c om*/
public static <T> List<T> resize(List<T> list, int newSize, T obj) {
    if (list instanceof ArrayList)
        ((ArrayList<T>) list).ensureCapacity(newSize);

    if (list.size() < newSize) {
        for (int ii = list.size(); ii < newSize; ii++)
            list.add(obj);
    } else if (list.size() > newSize) {
        if (list instanceof RandomAccess) {
            for (int ii = list.size() - 1; ii >= newSize; ii--)
                list.remove(ii);
        } else {
            ListIterator<T> itx = list.listIterator(newSize);
            while (itx.hasNext()) {
                itx.next();
                itx.remove();
            }
        }
    }

    return list;
}