Example usage for java.util Collection iterator

List of usage examples for java.util Collection iterator

Introduction

In this page you can find the example usage for java.util Collection iterator.

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this collection.

Usage

From source file:Main.java

public static <T> void add_news(Collection<T> target, Collection<? extends T> to_add) {
    for (Iterator<? extends T> it = to_add.iterator(); it.hasNext();) {
        T element = it.next();//ww  w  . ja  v  a 2s .  c  o m
        if (!target.contains(element))
            target.add(element);
    }
}

From source file:Main.java

public static <T> T getFirst(Collection<T> l) {
    return l.size() > 0 ? l.iterator().next() : null;
}

From source file:Main.java

public static void putAllInHashMapByGivenAttribute(HashMap map, Collection col, String attribName) {

    Iterator iter = col.iterator();
    while (iter.hasNext())
        putInHashMapByGivenAttribute(map, iter.next(), attribName);
}

From source file:Main.java

/**
 * Returns an arbitrary element from the given collection.
 * /*from   ww w  . j av  a2s . com*/
 * @param collection
 *        to retrieve the element from
 * @return an arbitrary element or <tt>null</tt> if the collection is empty
 */
public static <T> T getAny(final Collection<T> collection) {
    final Iterator<T> iterator = collection.iterator();
    if (iterator.hasNext()) {
        return iterator.next();
    }

    return null;
}

From source file:Main.java

public static <T> Optional<T> findAnyIn(Collection<T> collection) {
    final Iterator<T> iterator = collection.iterator();
    if (iterator.hasNext()) {
        return Optional.of(iterator.next());
    }//from   ww w.  jav a2 s.  co m
    return Optional.empty();
}

From source file:com.heliosdecompiler.helios.utils.Utils.java

public static <T> T find(T needle, Collection<T> haystack) {
    for (Iterator<T> iter = haystack.iterator(); iter.hasNext();) {
        T next = iter.next();//from w w  w . ja  v a  2 s.c  om
        if (next.equals(needle)) {
            return next;
        }
    }
    return null;
}

From source file:Main.java

public static Object getFirstItem(Collection<?> collection) {
    return null == collection || collection.isEmpty() ? null : collection.iterator().next();
}

From source file:Main.java

public static <T> T get(final Collection<T> coll, final int index) {
    final Iterator<T> it = coll.iterator();
    int i = 0;/*from   w w w .  j ava  2  s .  co  m*/
    T ret = null;
    while (it.hasNext()) {
        ret = it.next();
        if (i >= index)
            break;
        i++;
    }
    return ret;
}

From source file:Main.java

/**
 * Return the first element in {@code collection} that satisfies the given {@code predicate}.
 *
 * @param predicate  the test; must not be {@code null}.
 * @param collection the source of values; must not be {@code null}.
 * @return the value, or {@code null} if nothing matched.
 *//*from ww w .j a  v a 2s .  c  om*/
public static <T> T findFirst(Predicate<? super T> predicate, Collection<T> collection) {
    return find(checkNotNull(collection.iterator()), predicate, null);
}

From source file:Main.java

/**
 * @param coll {@link Collection} any collection object containing zero or more elements,can't be null.
 * @return {@link Number} the maximum value available in the collection.
 *//*www  . j  av a  2s . c  o  m*/
public static Number max(Collection<? extends Number> coll) {
    Iterator<? extends Number> i = coll.iterator();
    Number candidate = i.next();
    while (i.hasNext()) {
        Number next = i.next();
        if (next.doubleValue() - candidate.doubleValue() > 0)
            candidate = next;
    }
    return candidate;
}