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

/**
 * Removes all the given keys from the given map and adds their
 * corresponding values to the given collection.
 *
 * @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.
 * @param values A collection where the values corresponding to the given
 * keys are added.//www .  j  av a 2  s . co m
 * @note In case the target or the keys are not effective, nothing happens.
 * @note In case values is not effective, the keys are removed without
 * adding them.
 */
public static <TKey, TValue> void removeAll(final Map<TKey, TValue> target, final Iterable<? extends TKey> keys,
        Collection<? super TValue> values) {
    if (keys != null) {
        removeAll(target, keys.iterator(), values);
    }
}

From source file:lv.semti.Thesaurus.utils.JSONUtils.java

public static <E extends HasToJSON> String objectsToJSON(Iterable<E> l) {
    if (l == null)
        return "[]";
    StringBuilder res = new StringBuilder();
    res.append("[");
    Iterator<E> i = l.iterator();
    while (i.hasNext()) {
        res.append("{");
        res.append(i.next().toJSON());/*from ww  w.ja va  2 s . c o  m*/
        res.append("}");
        if (i.hasNext())
            res.append(", ");
    }
    res.append("]");
    return res.toString();
}

From source file:lv.semti.Thesaurus.utils.JSONUtils.java

public static <E> String simplesToJSON(Iterable<E> l) {
    if (l == null)
        return "[]";
    StringBuilder res = new StringBuilder();
    res.append("[");
    Iterator<E> i = l.iterator();
    while (i.hasNext()) {
        res.append("\"");
        res.append(JSONObject.escape(i.next().toString()));
        res.append("\"");
        if (i.hasNext())
            res.append(", ");
    }/*from w  ww  .j a v  a  2 s. co  m*/
    res.append("]");
    return res.toString();
}

From source file:Main.java

/**
 * Join collection to string with separator.
 * /*from w  w  w .ja v  a2 s . c o m*/
 * @param iterator
 * @param separator
 * @return array string
 */
public static String join(Iterable<?> iterable, String separator) {
    if (iterable == null) {
        return null;
    }

    Iterator<?> iterator = iterable.iterator();

    if (!iterator.hasNext()) {
        return "";
    }

    Object first = iterator.next();
    if (!iterator.hasNext()) {
        return first == null ? "" : first.toString();
    }

    StringBuilder buf = new StringBuilder(256);
    if (first != null) {
        buf.append(first);
    }

    while (iterator.hasNext()) {
        buf.append(separator);

        Object obj = iterator.next();
        if (obj != null) {
            buf.append(obj);
        }
    }

    return buf.toString();
}

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

public static <E extends Comparable<? super E>> E max(final Iterable<? extends E> it) {
    return Iterators2.max(it.iterator());
}

From source file:com.antsdb.saltedfish.util.CursorUtil.java

public static Cursor toCursor(CursorMeta meta, Iterable<?> it) {
    Cursor c = new IteratorCursor(meta, it.iterator());
    return c;/*from  ww w .  j  av a 2  s  .  c  om*/
}

From source file:Main.java

public static <T> Iterable<T> cycle(final Iterable<T> it) {
    return new Iterable<T>() {

        public Iterator<T> iterator() {
            return new Iterator<T>() {

                private Iterator<T> nextCycle = it.iterator();

                public boolean hasNext() {
                    return true; // forever cycles
                }/*from   ww w . java  2 s . c o  m*/

                public T next() {
                    if (!nextCycle.hasNext()) {
                        nextCycle = it.iterator(); // create the next cycle
                    }
                    return nextCycle.next();
                }
            };
        }
    };
}

From source file:Main.java

public static String join(Iterable<? extends Object> elements, CharSequence separator) {
    StringBuilder builder = new StringBuilder();

    if (elements != null) {
        Iterator<? extends Object> iter = elements.iterator();
        if (iter.hasNext()) {
            builder.append(String.valueOf(iter.next()));
            while (iter.hasNext()) {
                builder.append(separator).append(String.valueOf(iter.next()));
            }//from   ww  w. java2s  .  com
        }
    }

    return builder.toString();
}

From source file:Main.java

public static <T> Collection<Collection<T>> split(Iterable<T> coll, int size) {
    if (size < 1) {
        return Collections.emptyList();
    }/*from  w  w w  .  j  av a 2s  .c  o  m*/
    final List<Collection<T>> ret = new ArrayList<>();
    final Iterator<T> it = coll.iterator();
    Collection<T> box = null;
    for (int i = 0; it.hasNext(); ++i) {
        if (i % size == 0) {
            if (box != null) {
                ret.add(box);
            }
            box = new ArrayList<>(size);
        }
        //noinspection ConstantConditions
        box.add(it.next());
    }
    if (box != null) {
        ret.add(box);
    }
    return ret;
}

From source file:Main.java

/**
 * Return if the iterable is null or contains no elements.
 *
 * @param iterable// w w w.j a  v a 2 s  .  c  om
 * @return
 */
public static boolean isNullOrEmpty(Iterable<?> iterable) {
    if (iterable == null) {
        return true;
    }
    if (iterable instanceof Collection) {
        return ((Collection<?>) iterable).isEmpty();
    }
    return !iterable.iterator().hasNext();
}