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

public static <T> T getSingleElement(Collection<T> collection) {
    Iterator<T> iterator = collection.iterator();
    return iterator.hasNext() ? iterator.next() : null;
}

From source file:Main.java

public static <T extends Object> boolean isEqual(Collection<? extends T> coll, Comparator<? super T> comp) {

    Iterator<? extends T> i = coll.iterator();
    T first = i.next();
    while (i.hasNext()) {
        T next = i.next();// w ww . jav  a2 s.co  m
        if (comp.compare(first, next) < 0) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static <T extends Comparable<? super T>> T min(Iterator<? extends T> iterator) {
    T next, candidate = iterator.next();
    while (iterator.hasNext()) {
        if ((next = iterator.next()).compareTo(candidate) < 0) {
            candidate = next;/*from w w  w.  j a  va2 s . c  om*/
        }
    }
    return candidate;
}

From source file:Main.java

public static String join(Collection<?> coll, String delimiter) {
    if (coll.isEmpty()) {
        return "";
    }/*from w  ww. j  av  a2s .c  o  m*/
    Iterator<?> it = coll.iterator();
    String s = String.valueOf(it.next());
    while (it.hasNext()) {
        s += delimiter;
        s += String.valueOf(it.next());
    }
    return s;
}

From source file:MainClass.java

protected static void print(String label, Collection c) {

    System.out.println("--------------" + label + "--------------");

    Iterator it = c.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }//from   w w  w  . j  av  a  2  s  .c o m
}

From source file:Main.java

public static <T> String toString(Collection<T> c, String prefix, String sep, String suffix) {
    if (c == null || c.size() == 0)
        return prefix + suffix;
    Iterator<T> it = c.iterator();
    String result = prefix + it.next();
    while (it.hasNext())
        result += sep + it.next();//from   ww w.ja  va2  s .  c om
    return result + suffix;
}

From source file:Main.java

/**
 * Given an iterable A whose elements are iterables, this method will return the first
 * element of A.//from   w  w  w.j  av a2s.  c  o m
 * If no such element exist, an empty iterable is returned rather than null.
 *
 *
 * @param iterable
 * @param <S>
 * @param <T>
 * @return
 */
public static <S, T extends Iterable<S>> Iterable<S> safeGetFirst(Iterable<T> iterable) {
    Iterator<T> it = iterable.iterator();

    return it.hasNext() ? it.next() : Collections.<S>emptySet();
}

From source file:Main.java

public static <E> E getFirst(Collection<E> collection, E defaultValue) {
    Iterator<E> iterator = collection.iterator();
    return iterator.hasNext() ? iterator.next() : defaultValue;
}

From source file:Main.java

public static String nextOptionalArg(Iterator<String> argsIter, String defaultValue) {
    return argsIter.hasNext() ? argsIter.next() : defaultValue;
}

From source file:Main.java

public static void iterableToList(Iterable iterable, List list) {
    Iterator it = iterable.iterator();
    while (it.hasNext()) {
        list.add(it.next());
    }/*w  ww .j a  va2 s . c o m*/
}