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

/**
 * Calculates the number of generated elements of the given iterable.
 *
 * @param <T> The type of elements enumerated by the given iterable.
 * @param iterable The given iterable.//  w  ww.  j  av  a2  s  .  c om
 * @return The number of elements generated by the iterable.
 */
public static <T> int size(final Iterable<T> iterable) {
    if (iterable != null) {
        return size(iterable.iterator());
    } else {
        return 0x00;
    }
}

From source file:Main.java

public static <T> int compareAsIterable(Comparator<? super T> comparator, Iterable<? extends T> it1,
        Iterable<? extends T> it2) {
    Iterator<? extends T> elements2 = it2.iterator();
    for (T element1 : it1) {
        T element2;/*from  ww  w . j a va2 s  .  c  o m*/
        try {
            element2 = elements2.next();
        } catch (NoSuchElementException ex) {
            return 1;
        }
        int res = comparator.compare(element1, element2);
        if (res != 0)
            return res;
    }
    if (elements2.hasNext()) {
        return -1;
    }
    return 0;
}

From source file:Main.java

public static <T extends Comparable<? super T>> int compareAsIterable(Iterable<? extends T> it1,
        Iterable<? extends T> it2) {
    Iterator<? extends T> elements2 = it2.iterator();
    for (T element1 : it1) {
        T element2;//w ww . j av  a  2 s  .c  o m
        try {
            element2 = elements2.next();
        } catch (NoSuchElementException ex) {
            return 1;
        }
        int res = element1.compareTo(element2);
        if (res != 0)
            return res;
    }
    if (elements2.hasNext()) {
        return -1;
    }
    return 0;
}

From source file:Main.java

/**
 * Retrieves the nth element by linear search.
 * /* w ww .  java2 s .  c  o m*/
 * The first element has index 0.
 * 
 * @param iterable the iterable
 * @param n the 0-based index
 * @return the item
 */
public static <T> T nthOf(final Iterable<T> iterable, final int n) {
    final Iterator<T> iter = iterable.iterator();
    for (int i = 0; i < n; ++i) {
        iter.next();
    }
    return iter.next();
}

From source file:Main.java

public static <T> Iterable<T> closedIntervalIterable(Iterable<T> iterable, T start, T end) {
    return () -> closedIntervalIterator(iterable.iterator(), start, end);
}

From source file:Main.java

/**
 * Concatenates the elements from the collection to one string.
 * If the collection is empty an empty string is returned.
 *
 * @param collection collection/*from www  . ja v a  2  s  . com*/
 * @param delimiter delimiter between the elements
 * @return concatenation
 */
public static String join(Iterable collection, String delimiter) {
    return join(collection.iterator(), delimiter);
}

From source file:edu.byu.nlp.util.Counters.java

public static <E> Counter<E> identityCount(Iterable<E> it) {
    return identityCount(it.iterator());
}

From source file:Main.java

public static <T> int compareAsIterable(@Nonnull Comparator<? super T> comparator,
        @Nonnull Iterable<? extends T> it1, @Nonnull Iterable<? extends T> it2) {
    Iterator<? extends T> elements2 = it2.iterator();
    for (T element1 : it1) {
        T element2;/*from  w  w  w .  ja  v a 2  s .  c o  m*/
        try {
            element2 = elements2.next();
        } catch (NoSuchElementException ex) {
            return 1;
        }
        int res = comparator.compare(element1, element2);
        if (res != 0)
            return res;
    }
    if (elements2.hasNext()) {
        return -1;
    }
    return 0;
}

From source file:Main.java

public static <T extends Comparable<? super T>> int compareAsIterable(@Nonnull Iterable<? extends T> it1,
        @Nonnull Iterable<? extends T> it2) {
    Iterator<? extends T> elements2 = it2.iterator();
    for (T element1 : it1) {
        T element2;/*from  w ww  .j ava2s  .  c  om*/
        try {
            element2 = elements2.next();
        } catch (NoSuchElementException ex) {
            return 1;
        }
        int res = element1.compareTo(element2);
        if (res != 0)
            return res;
    }
    if (elements2.hasNext()) {
        return -1;
    }
    return 0;
}

From source file:Main.java

public static <T> T getSingleIfExist(Iterable<T> iterable) {
    if (iterable == null)
        return null;
    final Iterator<? extends T> iter = iterable.iterator();
    if (!iter.hasNext())
        return null;
    final T result = iter.next();
    if (iter.hasNext()) {
        throw new IllegalArgumentException("More than (expected) one element in " + iterable);
    }//from  ww  w .j a  v a2  s  .co  m
    return result;
}