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

/**
 * Joins a collection of strings to a single string, all parts are merged with a delimiter string.
 *
 * @param aContainer//from w  w w . j av a2  s  . co  m
 * @param aDelimiter
 * @return
 */
public static String join(final Iterable<String> aContainer, final String aDelimiter) {
    StringBuilder builder = new StringBuilder();
    Iterator<String> it = aContainer.iterator();
    while (it.hasNext()) {
        builder.append(it.next());
        if (it.hasNext())
            builder.append(aDelimiter);
    }
    return builder.toString();
}

From source file:com.griddynamics.jagger.providers.csv.CSVProviderTest.java

private static void testIterable(Iterable<RequestPath> i, RequestPath[] requestPaths) {
    Iterator<RequestPath> it = i.iterator();
    Assert.assertEquals(it.hasNext(), true);
    Assert.assertEquals(it.hasNext(), true);
    Assert.assertEquals(it.next(), requestPaths[0]);
    Assert.assertEquals(it.hasNext(), true);
    Assert.assertEquals(it.next(), requestPaths[1]);
    Assert.assertEquals(it.hasNext(), true);
    Assert.assertEquals(it.next(), requestPaths[2]);
    Assert.assertEquals(it.hasNext(), false);
    try {/*from w w  w  .j  a v  a2  s  . c  o  m*/
        it.next();
        Assert.fail("Iterator must throws NoSuchElementException at empty data set!");
    } catch (NoSuchElementException e) {
        //OK
    }
    Assert.assertEquals(it.hasNext(), false);
}

From source file:Main.java

/** 
 * Answers true if a predicate is true for at least one element of a collection.
 * <p>/*from  w w w  .  j a v  a  2  s  .  com*/
 * A <code>null</code> collection or predicate returns false.
 * 
 * @param collection the collection to get the input from, may be null
 * @param predicate the predicate to use, may be null
 * @return true if at least one element of the collection matches the predicate
 */
public static <T> boolean exists(Iterable<T> collection, Predicate<? super T> predicate) {
    if (collection != null && predicate != null) {
        for (Iterator<T> it = collection.iterator(); it.hasNext();) {
            if (predicate.apply(it.next())) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

/**
 * Return a List containing all the elements of the specified Iterable that compare as being
 * equal to the maximum element.//from ww  w.ja  v a2 s .  co m
 *
 * @throws NoSuchElementException if the Iterable is empty.
 */
public static <T> List<T> maxList(Iterable<T> iterable, Comparator<? super T> comp) {
    Iterator<T> itr = iterable.iterator();
    T max = itr.next();
    List<T> maxes = new ArrayList<T>();
    maxes.add(max);
    if (itr.hasNext()) {
        do {
            T elem = itr.next();
            int cmp = comp.compare(max, elem);
            if (cmp <= 0) {
                if (cmp < 0) {
                    max = elem;
                    maxes.clear();
                }
                maxes.add(elem);
            }
        } while (itr.hasNext());

    } else if (0 != comp.compare(max, max)) {
        // The main point of this test is to compare the sole element to something,
        // in case it turns out to be incompatible with the Comparator for some reason.
        // In that case, we don't even get to this IAE, we've probably already bombed out
        // with an NPE or CCE. For example, the Comparable version could be called with
        // a sole element of null. (Collections.max() gets this wrong.)
        throw new IllegalArgumentException();
    }
    return maxes;
}

From source file:Main.java

public static <S, T> Iterable<T> mapIterable(final Iterable<? extends S> delegate,
        final Function<? super S, ? extends T> mapping) {
    return () -> mapIterator(delegate.iterator(), mapping);
}

From source file:de.tudarmstadt.ukp.wikipedia.util.GraphUtilities.java

public static Set<Integer> getRandomPageSubset(Iterable<Page> pages, int pResultSetSize) {
    Set<Integer> pageIDs = new HashSet<Integer>();
    while (pages.iterator().hasNext()) {
        pageIDs.add(pages.iterator().next().getPageId());
    }/*from w  w w .  j a  v a2s . c  o m*/
    return getRandomPageSubset(pageIDs, pResultSetSize);
}

From source file:Main.java

public static <T> Iterable<T> concat(Iterable<T> first, Iterable<T> second) {

    return new Iterable<T>() {

        final Iterator<T> it1 = first.iterator();
        final Iterator<T> it2 = second.iterator();

        @Override//  www . jav  a2s .  c  om
        public Iterator<T> iterator() {

            return new Iterator<T>() {

                Iterator<T> curr;

                @Override
                public boolean hasNext() {
                    if (it1.hasNext()) {
                        curr = it1;
                        return true;
                    } else if (it2.hasNext()) {
                        curr = it2;
                        return true;
                    }
                    return false;
                }

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

From source file:Main.java

/**
 * Removes all the given keys from the given map.
 *
 * @param <TKey> The type of the keys of the map.
 * @param <TValue> The types of the values of the map.
 * @param target The map to remove the specified keys from.
 * @param keys An iterable providing the keys to be removed.
 * @note In case the target or the keys are not effective, nothing happens.
 *///from  w w w  .j a va2 s .  c  o m
public static <TKey, TValue> void removeAll(final Map<TKey, TValue> target,
        final Iterable<? extends TKey> keys) {
    if (keys != null) {
        removeAll(target, keys.iterator());
    }
}

From source file:Main.java

/**
 * there's no point in being "type safe" in this method, as the returned value is not typed, and we would like to
 * support any type of iterable comparison.
 *
 * @param suspectedPrefix some iterable whose may be a prefix for container
 * @param container the iterable who may or may not start with suspectedPrefix
 * @return true iff suspectedPrefix is a prefix of container
 *
 *///  w  w  w . ja  v  a  2 s .c o m
public static boolean isPrefix(Iterable suspectedPrefix, Iterable container) {

    final Iterator spi = suspectedPrefix.iterator();
    final Iterator ci = container.iterator();

    //the function consumes both iterators from left to right
    while (true) {

        //if we finished consuming the entire prefix - great!
        if (!spi.hasNext()) {
            return true;
        }

        //if prefix hasn't been consumed entirely, but the collection has - bad!
        if (!ci.hasNext()) {
            return false;
        }

        //if both still have not been completely consumed, but there's a difference - bad!
        if (areDifferent(spi.next(), ci.next())) {
            return false;
        }
    }
}

From source file:Main.java

private static boolean listContentsEquals(Iterable<?> l1, Iterable<?> l2) {
    Iterator<?> iter1 = l1.iterator();
    Iterator<?> iter2 = l2.iterator();
    while (iter1.hasNext()) {
        if (!iter2.hasNext()) {
            return false;
        }/*from w w w.  j  a v  a 2  s  . c  o  m*/
        Object e1 = iter1.next();
        Object e2 = iter2.next();
        if ((e1 == l1 && (e2 == l1 || e2 == l2)) || (e1 == l2 && (e2 == l1 || e2 == l2))) {
            // handle case where list contains itself - don't overflow stack
            continue;
        }
        if (e1 == null ? e2 != null : !e1.equals(e2)) {
            return false;
        }
    }
    if (iter2.hasNext()) {
        return false;
    }
    return true;
}