Example usage for java.lang Iterable iterator

List of usage examples for java.lang Iterable iterator

Introduction

In this page you can find the example usage for java.lang Iterable iterator.

Prototype

Iterator<T> iterator();

Source Link

Document

Returns an iterator over elements of type T .

Usage

From source file:Main.java

public static <T> List<T> read(Iterable<T> iterable) {
    return read(iterable.iterator());
}

From source file:Main.java

public static <A, B> Iterable<Map.Entry<A, B>> zip(Iterable<A> a, Iterable<B> b) {
    Iterator<A> ai = a.iterator();
    Iterator<B> bi = b.iterator();

    List<Map.Entry<A, B>> result = new ArrayList<>();
    while (ai.hasNext() && bi.hasNext())
        result.add(Maps.immutableEntry(ai.next(), bi.next()));

    return result;
}

From source file:Main.java

public static final <T> List<T> collect(Iterable<? extends T> it) {
    return collect(it.iterator());
}

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 ww w  . j a  v  a  2s.c  o m
    Iterator<T> it = source.iterator();
    while (it.hasNext()) {
        result = it.next();
        // source
    }
    return result;

}

From source file:Main.java

public static <E> E getOnlyElement(Iterable<E> iterable) {
    Iterator<E> iterator = iterable.iterator();

    if (!iterator.hasNext()) {
        throw new RuntimeException("Collection is empty");
    }//from   w w w.j  a v  a2 s .c  o  m

    E element = iterator.next();

    if (iterator.hasNext()) {
        throw new RuntimeException("Collection contains more than one item");
    }

    return element;
}

From source file:Main.java

/**
 * From http://stackoverflow.com/questions/3047051/how-to-determine-if-a-list-is-sorted-in-java
 * @param iterable/*from   ww  w. ja v  a 2  s .  c om*/
 * @param <T>
 * @return
 */
public static <T extends Comparable<? super T>> boolean isSorted(Iterable<T> iterable) {
    Iterator<T> iter = iterable.iterator();
    if (!iter.hasNext()) {
        return true;
    }
    T t = iter.next();
    while (iter.hasNext()) {
        T t2 = iter.next();
        if (t.compareTo(t2) > 0) {
            return false;
        }
        t = t2;
    }
    return true;
}

From source file:Main.java

public static <E> void addAll(Collection<E> collection, Iterable<? extends E> iterable) {
    addAll(collection, iterable.iterator());
}

From source file:Main.java

public static <T> int indexOfSame(Iterable<? extends T> iterable, T obj) {
    return indexOfSame(iterable.iterator(), obj);
}

From source file:Main.java

public static <T> String join(Iterable<T> iterable, String d) {
    Iterator it = iterable.iterator();
    if (!it.hasNext()) {
        return "";
    }/*from www .  j  a  v  a 2s  .  co m*/
    StringBuilder sb = new StringBuilder();
    String prefix = "";
    while (it.hasNext()) {
        sb.append(prefix);
        prefix = d;
        sb.append(it.next());
    }
    return sb.toString();
}

From source file:Main.java

/** @return whether given coll contains given obj 
 * (obj must be the same as the one contained, not just equal). 
 *///w  ww  . j a  va2 s . c o  m
public static boolean containsSame(Iterable<?> coll, Object obj) {
    for (Iterator<?> iterator = coll.iterator(); iterator.hasNext();) {
        Object element = iterator.next();
        if (element == obj)
            return true;
    }
    return false;
}