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> T getSingleElement(Collection<T> collection) {
    Iterator<T> iterator = collection.iterator();
    return iterator.hasNext() ? iterator.next() : null;
}

From source file:Main.java

static public <T> void removeNulls(Collection<T> list) {
    for (Iterator<T> iter = list.iterator(); iter.hasNext();) {
        T e = iter.next();/*from w ww .ja  va  2  s  .  com*/
        if (e == null)
            iter.remove();
    }
}

From source file:Main.java

public static void removeKeys(Collection keys, Map map) {
    for (Iterator i = keys.iterator(); i.hasNext();) {
        Object key = (Object) i.next();
        map.remove(key);/*w  w w.j  a va  2  s  .  c  o  m*/
    }
}

From source file:Main.java

public static <T> boolean removeIdentity(Collection<T> c, T e) {
    for (Iterator<T> i = c.iterator(); i.hasNext();)
        if (i.next() == e) {
            i.remove();//from w w  w  .j  a v a 2s  .c  o m
            return true;
        }
    return false;
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <E> E maximumValue(Collection<E> c) {
    Iterator<E> iterator = c.iterator();
    E e = iterator.next();/*from  w w w .  j a  v a2 s. com*/
    while (iterator.hasNext()) {
        E current = iterator.next();
        if (((Comparable) e).compareTo((Comparable) current) > 0) {
            e = current;
        }
    }
    return e;
}

From source file:CollectionAll.java

static void traverse(Collection coll) {
    Iterator iter = coll.iterator();
    while (iter.hasNext()) {
        String elem = (String) iter.next();
        System.out.print(elem + " ");
    }/*from  ww  w.ja va2 s .c om*/
    System.out.println();
}

From source file:Main.java

public static Object getFirstObject(Collection<?> collection) {
    Iterator<?> iterator = collection.iterator();

    if (iterator.hasNext()) {
        return iterator.next();
    } else {//  w w w. j  ava  2 s  .  c  om
        return null;
    }
}

From source file:Main.java

/** @return whether given coll contains all elements of other, using a naive/basic algorithm. */
public static boolean containsAll(Collection<?> coll, Collection<?> other) {
    Iterator<?> citer = other.iterator();
    while (citer.hasNext())
        if (!coll.contains(citer.next()))
            return false;
    return true;/*from www  .j  ava 2  s  .  c  o  m*/
}

From source file:Main.java

/** Returns true if all elements of c match obj */
public static boolean all(Collection c, Object obj) {
    for (Iterator iterator = c.iterator(); iterator.hasNext();) {
        Object o = iterator.next();
        if (!o.equals(obj))
            return false;
    }/*from ww  w.j a  va 2 s  .c  o m*/
    return true;
}

From source file:Main.java

public static <T> T removeAny(Collection<T> collection) {
    T item = null;/*from w  ww.j ava  2s  .c  o m*/

    if (collection.iterator().hasNext())
        item = collection.iterator().next();

    return item;
}