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

/**
 * Compare if two iterable objects are equal by element.
 * @param i1 sequence 1./*from w ww  .ja  v a  2s  .  c o  m*/
 * @param i2 sequence 2.
 * @param <T> type.
 * @return true if all elements at the same index are equal.
 */
public static <T> boolean iterableEquals(Iterable<T> i1, Iterable<T> i2) {

    if (Objects.equals(i1, i2))
        return true;

    if (i1 == null || i2 == null)
        return false;

    Iterator<T> iterator1 = i1.iterator();
    Iterator<T> iterator2 = i2.iterator();

    while (iterator1.hasNext()) {

        if (!iterator2.hasNext())
            return false;

        T obj1 = iterator1.next();
        T obj2 = iterator2.next();

        if (!Objects.equals(obj1, obj2)) {
            return false;
        }
    }

    return !iterator2.hasNext();
}

From source file:Main.java

public static Element getChild(Element e, String path) throws Exception {
    Iterable<Node> list = eval(e, path);
    if (list == null) {
        return null;
    }//from   w w w  .  j av a  2s.  c om
    Iterator<Node> iterator = list.iterator();
    return (Element) (iterator.hasNext() ? iterator.next() : null);
}

From source file:Main.java

public static String implode(final Iterable<?> pieces, final String glue) {
    if (pieces == null) {
        return "";
    }//from  www .jav a 2 s  .  c  om
    final StringBuilder resBuf = new StringBuilder(100);
    final Iterator<?> iter = pieces.iterator();
    resBuf.append(iter.next().toString());
    while (iter.hasNext()) {
        resBuf.append(glue);
        resBuf.append(iter.next().toString());
    }
    return resBuf.toString();
}

From source file:com.splicemachine.db.impl.ast.CorrelatedPushDown.java

public static <K, V> Map<K, V> zipMap(Iterable<K> keys, Iterable<V> vals) {
    Map<K, V> m = new HashMap<K, V>();
    Iterator<V> valsIterator = vals.iterator();
    for (K key : keys) {
        if (valsIterator.hasNext()) {
            m.put(key, valsIterator.next());
        }/*from   w  w  w.jav  a  2s  .  c  o  m*/
    }
    return m;
}

From source file:com.github.rvesse.airline.restrictions.options.MutuallyExclusiveRestriction.java

private static String toOptionsList(Iterable<OptionMetadata> options) {
    StringBuilder builder = new StringBuilder();
    Iterator<OptionMetadata> ops = options.iterator();
    while (ops.hasNext()) {
        OptionMetadata option = ops.next();

        Iterator<String> names = option.getOptions().iterator();
        while (names.hasNext()) {
            builder.append(names.next());
            if (names.hasNext() || ops.hasNext())
                builder.append(", ");
        }//from www.j a v  a 2 s.  c  om
    }
    return builder.toString();
}

From source file:Main.java

/**
 * Performs a toString on each element given iterable with a given delimiter
 * between elements./*from   w ww.j ava 2s  . c o  m*/
 *
 * @param   list
 *      The list to call toString on each element for.
 * @param   delimiter
 *      The delimiter.
 * @return
 *      A string with the toString on each element in the list called with
 *      a given delimiter between elements. If null is given, then
 *      "null" is returned. If an empty list is given, "" is returned.
 */
public static String toStringDelimited(final Iterable<?> list, final String delimiter) {
    if (list == null) {
        return "null";
    }

    final StringBuffer result = new StringBuffer();
    final Iterator<?> iterator = list.iterator();

    if (iterator.hasNext()) {
        result.append(iterator.next());
    }

    while (iterator.hasNext()) {
        result.append(delimiter);
        result.append(iterator.next());
    }

    return result.toString();
}

From source file:net.dontdrinkandroot.utils.lang.StringUtils.java

public static <T> CharSequence join(final Iterable<T> objects, final String seperator,
        final Transformer<T, String> toStringTransformer) {

    final StringBuffer sb = new StringBuffer();
    final Iterator<T> iterator = objects.iterator();
    while (iterator.hasNext()) {
        final T object = iterator.next();
        sb.append(toStringTransformer.transform(object));
        if (iterator.hasNext()) {
            sb.append(seperator);//from www. j a v  a  2  s . co  m
        }
    }

    return sb;
}

From source file:com.github.rvesse.airline.utils.AirlineUtils.java

public static <T> T last(Iterable<T> iterable) {
    return last(iterable.iterator(), null);
}

From source file:com.github.rvesse.airline.utils.AirlineUtils.java

public static <T> T first(Iterable<T> iterable) {
    return first(iterable.iterator(), null);
}

From source file:Main.java

public static String join(final Iterable<?> iterable, String separator) {

    // handle null, zero and one elements before building a buffer
    if (iterable == null) {
        return null;
    }/*from   w w  w . java2  s  . c o  m*/
    Iterator<?> iterator = iterable.iterator();
    if (!iterator.hasNext()) {
        return "";
    }
    final Object first = iterator.next();
    if (!iterator.hasNext()) {
        return first.toString();
    }

    // two or more elements
    final StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
    if (first != null) {
        buf.append(first);
    }

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

    return buf.toString();
}