Example usage for java.util Iterator next

List of usage examples for java.util Iterator next

Introduction

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

Prototype

E next();

Source Link

Document

Returns the next element in the iteration.

Usage

From source file:Main.java

/**
 * Adds all elements in the iteration to the given collection.
 * //w w w. j  a v a  2 s .com
 * @param collection  the collection to add to, must not be null
 * @param iterator  the iterator of elements to add, must not be null
 * @throws NullPointerException if the collection or iterator is null
 */
public static <T> void addAll(Collection<T> collection, Iterator<T> iterator) {
    while (iterator.hasNext()) {
        collection.add(iterator.next());
    }
}

From source file:Main.java

public static <T> T removeFrom(Iterable<T> collection, Predicate<T> predicate) {
    Iterator<T> it = collection.iterator();

    while (it.hasNext()) {
        T o = it.next();

        if (predicate.test(o)) {
            it.remove();//from  w ww . j ava  2s.c om
            return o;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Removes nulls from provided collection.
 * //from ww w . j  av  a2  s  .  c o  m
 * @param c
 *            to remove nulls from
 * @return number of removed elements
 */
@SuppressWarnings("rawtypes")
public static int removeNulls(Collection c) {
    int removed = 0;
    Iterator iterator = c.iterator();
    while (iterator.hasNext()) {
        if (iterator.next() == null) {
            iterator.remove();
            removed++;
        }
    }
    return removed;
}

From source file:Main.java

public static <T> void cleanNulls(Collection<T> c) {
    Iterator<T> it = c.iterator();
    try {//from   www  . j  ava  2 s.c  o  m
        while (it.hasNext())
            if (it.next() == null)
                c.remove(null);
    } catch (NullPointerException e) {
        // Does not permit nulls, it should be ok
    }
}

From source file:Main.java

public static <T> void addArrayListToArrayList(ArrayList<T> array, ArrayList<T> arrayList) {
    Iterator<T> it = array.iterator();
    while (it.hasNext()) {
        arrayList.add(it.next());
    }//from w w  w. j  a v a 2s .  co  m
}

From source file:Main.java

public static <T> T lastOrDefault(Iterable<T> source) {
    if (source == null || !source.iterator().hasNext())
        return null;
    T result = null;/*from   w  w w.  j  a va  2  s.  co  m*/
    Iterator<T> it = source.iterator();
    while (it.hasNext()) {
        result = it.next();
        // source
    }
    return result;

}

From source file:Main.java

public static boolean remove(Collection collection, Object object) {
    boolean removed = false;
    Iterator it = collection.iterator();
    while (it.hasNext()) {
        Object o = it.next();
        if (o.equals(object)) {
            removed = true;//  w w  w. j av a 2  s . c  om
            it.remove();
            break;
        }
    }

    return removed;
}

From source file:Main.java

public static <ELEM, COLL extends Collection<? super ELEM>> COLL addAllFromIterator(COLL dest,
        Iterator<? extends ELEM> source) {
    while (source.hasNext()) {
        dest.add(source.next());
    }/*ww w  .  j a  va 2 s  .  c o m*/
    return dest;
}

From source file:Main.java

public static void printParameter(NetworkInterface ni) throws SocketException {
    System.out.println(" Name = " + ni.getName());
    System.out.println(" Display Name = " + ni.getDisplayName());
    System.out.println(" Is up = " + ni.isUp());
    System.out.println(" Support multicast = " + ni.supportsMulticast());
    System.out.println(" Is loopback = " + ni.isLoopback());
    System.out.println(" Is virtual = " + ni.isVirtual());
    System.out.println(" Is point to point = " + ni.isPointToPoint());
    System.out.println(" Hardware address = " + ni.getHardwareAddress());
    System.out.println(" MTU = " + ni.getMTU());

    System.out.println("\nList of Interface Addresses:");
    List<InterfaceAddress> list = ni.getInterfaceAddresses();
    Iterator<InterfaceAddress> it = list.iterator();

    while (it.hasNext()) {
        InterfaceAddress ia = it.next();
        System.out.println(" Address = " + ia.getAddress());
        System.out.println(" Broadcast = " + ia.getBroadcast());
        System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength());
        System.out.println("");
    }/*  w  w w.j a  v a  2  s. c om*/
}

From source file:Main.java

public static void showList(List<? extends Object> lst) {
    Iterator<? extends Object> it = lst.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }/* www  . j  av  a  2s. c om*/
}