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 String toString(Iterable<Object> pathItrb) {
    LinkedList<Object> list = new LinkedList<Object>();
    Iterator itr = pathItrb.iterator();
    while (itr.hasNext()) {
        list.addFirst(itr.next());/*from   w  w  w.j  a  v a 2 s  . c o  m*/
    }
    //
    StringBuilder sb = new StringBuilder();
    boolean isFirst = true;
    for (Object obj : list) {
        if (isFirst) {
            isFirst = false;
        } else {
            sb.append("/"); // NOI18N
        }
        sb.append(obj.toString());
    }
    //
    return sb.toString();
}

From source file:Main.java

public static <T> int sum(Iterable<T> source, Function<T, Integer> func) {
    // TODO Auto-generated method stub
    if (source == null || !source.iterator().hasNext())
        return 0;
    Iterator<T> it = source.iterator();
    int count = 0;
    for (T item : source) {
        count += func.apply(item);/*from   w  ww.  j a  va  2  s  .  c o  m*/
    }
    /*for (T item = it.next(); it.hasNext();it.) {
       count += func.apply(item);
    }*/
    return count;

}

From source file:Main.java

/**
 * Returns 0 if empty iterable//  w  ww  .  ja va  2s. c o  m
 * */
public static double computeAverage(Iterable<Double> iterable) {
    synchronized (iterable) {
        Iterator<Double> iterator = iterable.iterator();
        int numberOfElements = 0;
        double sum = 0;
        while (iterator.hasNext()) {
            Double value = iterator.next();
            // FIXME the point is: why next() return null if the addition of
            // null objects is prevented in the circular fifo queue?
            // Investigate the question...
            if (value == null) {
                iterator.remove();
                System.out.println("null in iterator. sum: " + sum);
            } else {
                sum += value;
                numberOfElements++;
            }
        }
        // System.out.println("sum: " + sum + "; numner of elements: " +
        // numberOfElements);
        if (numberOfElements == 0) {
            return 0;
        }
        return sum / numberOfElements;
    }
}

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   w w w  .  j av  a 2s .  c om*/
 * @param start start string
 * @param delimiter delimiter between the elements
 * @param end end string
 * @return concatenation
 */
public static String join(Iterable collection, String start, String delimiter, String end) {
    return join(collection.iterator(), start, delimiter, end);
}

From source file:Main.java

public static <X, Y> Map<X, Y> map(List<X> keys, Iterable<Y> values) {
    Map<X, Y> ret = new HashMap<X, Y>();
    Iterator<X> ks = keys.iterator();
    Iterator<Y> vs = values.iterator();
    while (ks.hasNext()) {
        ret.put(ks.next(), vs.next());//from  w  ww .  jav  a  2s.c  om
    }
    if (vs.hasNext()) {
        throw new RuntimeException("values is greater than keys");
    }
    return ret;
}

From source file:Main.java

public static String toString(Iterable<?> it, String separator) {
    final StringBuilder builder = new StringBuilder();
    final Iterator<?> iterator = it.iterator();
    while (iterator.hasNext()) {
        builder.append(iterator.next().toString());
        if (iterator.hasNext()) {
            builder.append(separator);/*from  w  ww.  j  av  a2s . c  om*/
        }
    }
    return builder.toString();
}

From source file:Main.java

public static <V> String join(String prefix, boolean bQuote, String seprator, Iterable<V> coll) {
    StringBuffer sb = new StringBuffer(prefix);
    for (Iterator<V> iterator = coll.iterator(); iterator.hasNext();) {
        V v = iterator.next();//w  w  w .ja  v  a  2 s. c om
        if (bQuote)
            sb.append("'");
        sb.append(v.toString());
        if (bQuote)
            sb.append("'");
        if (iterator.hasNext())
            sb.append(seprator);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Repeats the same iterable over and over.
 * @param iterable to repeat/*from  w ww. j  a v a 2s  .  c  o m*/
 * @param n specifies how often the iterable is repeated
 * @return n times repeated iterbale
 */
public static <T> Iterable<T> cycle(Iterable<T> iterable, int n) {
    return () -> new Iterator<T>() {
        private Iterator<T> iterator = iterable.iterator();
        private int count = 0;

        @Override
        public boolean hasNext() {
            if (!iterator.hasNext()) {
                iterator = iterable.iterator();
                count++;
            }
            return n < 0 || count < n;
        }

        @Override
        public T next() {
            return iterator.next();
        }
    };
}

From source file:Main.java

public static String toPathString(Iterable<File> files, String separator) {
    final StringBuilder builder = new StringBuilder();
    final Iterator<File> fileIt = files.iterator();
    while (fileIt.hasNext()) {
        builder.append(fileIt.next().getAbsolutePath());
        if (fileIt.hasNext()) {
            builder.append(separator);/*from   w w w.j av  a2  s. c  o  m*/
        }
    }
    return builder.toString();
}

From source file:Main.java

/**
 * @param iterable//from  w  w w . java  2  s .  c o  m
 * @return null if the iterable element is null or if there are no elements in it or the first element if it does
 */
public static <E> E first(final Iterable<E> iterable) {
    if (null == iterable) {
        return null;
    }
    final Iterator<E> it = iterable.iterator();
    if (!it.hasNext()) {
        return null;
    }
    return it.next();
}