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:edu.byu.nlp.util.Iterables2.java

public static <E> List<E> topN(final Iterable<? extends E> it, final Comparator<? super E> c, final int n) {
    return Iterators2.topN(it.iterator(), n, c);
}

From source file:Main.java

public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) {
    if (elements instanceof Collection) {
        Collection<? extends E> collection = (Collection<? extends E>) elements;
        return new ArrayList<E>(collection);
    } else {//from w  w  w.j a va2 s.  c  o  m
        return newArrayList(elements.iterator());
    }
}

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

public static <E> Iterable<E> subInterval(final Iterable<E> it, final int start, final int length) {
    return Lists.newArrayList(Iterators2.subInterval(it.iterator(), start, length));
}

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

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

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

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

From source file:Main.java

/**
 * Gets the first element from an iterable. If the iterable is null or
 * empty, null is returned.//  ww w  .  java2 s.  c  o  m
 *
 * @param   <T>
 *      The type of element.
 * @param   iterable
 *      The iterable to get the first element from.
 * @return
 *      The first element from the iterable, if one exists. Otherwise,
 *      null.
 */
public static <T> T getFirst(final Iterable<? extends T> iterable) {
    if (iterable == null) {
        // No first element.
        return null;
    }

    final Iterator<? extends T> iterator = iterable.iterator();
    if (iterator.hasNext()) {
        return iterator.next();
    } else {
        // No first element.
        return null;
    }
}

From source file:Main.java

/**
 * Converts a collection of objects into a human readable list. For example
 * ["item1"] = "item1"/*w  w  w  . j av a  2 s  .  c om*/
 * ["item1","item2"] = "item1 and item2"
 * ["item1","item2","item3"] = "item1, item2 and item3"
 * @param iterable a collection of LabelValue objects
 * @param conjunction the conjunction for the sentence (eg "and", "or", "nor")
 * @return a string representing a human readable list.
 */
public static String toSentence(Iterable<?> iterable, String conjunction) {
    if (iterable == null) {
        return null;
    }

    StringBuilder sentence = new StringBuilder();

    boolean firstItem = true;

    for (Iterator<?> it = iterable.iterator(); it.hasNext();) {
        Object label = it.next();

        if (!firstItem) {
            sentence.append(it.hasNext() ? "," : " " + conjunction).append(' ');
        } else {
            firstItem = false;
        }

        sentence.append(label);
    }

    return sentence.toString();
}

From source file:hu.webhejj.commons.text.StringUtils.java

/** append values to buf separated with the specified separator */
public static void join(StringBuilder buf, Iterable<?> values, String separator) {
    for (Iterator<?> i = values.iterator(); i.hasNext();) {
        buf.append(i.next());//www  .  j av  a 2 s. co m
        if (i.hasNext()) {
            buf.append(separator);
        }
    }
}

From source file:elaborate.editor.solr.ElaborateSolrIndexer.java

private static void handleMultiValuedFields(String facetName, String multiValue, SolrInputDocument doc) {
    Log.info("facetName={}", facetName);
    doc.removeField(facetName);/*from   w ww.  j  av  a2  s  .c  o m*/
    Iterable<String> values = StringUtil.getValues(multiValue);
    if (!values.iterator().hasNext()) {
        doc.addField("mv_" + facetName, EMPTYVALUE_SYMBOL, 1.0f);
    } else {
        for (String value : values) {
            if (StringUtils.isNotBlank(value)) {
                doc.addField("mv_" + facetName, value, 1.0f);
            }
        }
    }
}

From source file:Main.java

/**
 * Removes all available elements from this {@link Iterable} and adds 
 * them to the given {@link Collection}.
 *///from w w w  .  jav  a2  s .c  om
public static <T> int drainTo(Iterable<? extends T> src, Collection<? super T> dst, int max) {
    if (src instanceof BlockingQueue<?>) {
        return ((BlockingQueue<? extends T>) src).drainTo(dst, max);
    }

    int count = 0;

    for (Iterator<? extends T> it = src.iterator(); it.hasNext();) {
        if (count >= max) {
            break;
        }

        dst.add(it.next());
        it.remove();
        ++count;
    }

    return count;
}