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 boolean remove(Collection collection, Object object) {
    boolean removed = false;
    Iterator it = collection.iterator();
    while (it.hasNext()) {
        Object o = it.next();/*from  w ww .jav a  2s . com*/
        if (o.equals(object)) {
            removed = true;
            it.remove();
            break;
        }
    }

    return removed;
}

From source file:Main.java

/**
 * Answer whether or not coll contains Object o, 
 * but not based on equal(), but on compareTo()==0
 * @param coll//  w w  w  .  j  a  va2  s.  c  om
 * @param o
 * @param c
 * @return
 */
public static boolean contains(Collection coll, Object o, Comparator c) {
    boolean answer = false;
    for (Iterator i = coll.iterator(); i.hasNext() && !answer;) {
        answer = 0 == c.compare(o, i.next());
    }
    return answer;
}

From source file:Main.java

public static <E> E at(Collection<E> collection, int index) throws Exception {

    Iterator<E> itr = collection.iterator();

    for (int i = 0; itr.hasNext(); i++) {
        E element = itr.next();/*from w ww.j  a v  a2 s.  c  om*/

        if (i == index) {
            return element;
        }
    }

    return null;
}

From source file:Main.java

public static <T> Collection<? extends T> split(Collection<? extends T> d, int n) {
    Collection<T> tmp = new ArrayList<>();
    Iterator it = d.iterator();
    int k = Math.max(0, n);
    while (it.hasNext() && k > 0) {
        tmp.add((T) it.next());//from ww  w.ja va  2 s  . com
        k--;
    }
    return tmp;
}

From source file:Main.java

public static void removeItemFromCollectionByUsingIteratorRemoveMethod(Collection<String> collection,
        String elementToBeRemoved) {

    Iterator iterator = collection.iterator();

    while (iterator.hasNext()) {
        String element = (String) iterator.next();

        if (element.equals(elementToBeRemoved)) {
            iterator.remove();/*from   w w  w  .java  2s.c o m*/
        }
    }
}

From source file:Main.java

public static void filterNull(Collection<?> collection) {
    if (!isEmpty(collection)) {
        for (Iterator<?> it = collection.iterator(); it.hasNext();) {
            if (it.next() == null) {
                it.remove();// ww  w.ja  v a  2s  .co m
            }
        }
    }
}

From source file:Main.java

/**
 * @return the first element of given coll, if collection is non-null and non-empty. Otherwise null. 
 *///from   ww w.j  a  v  a 2 s . co m
public static <T> T getFirstElementOrNull(Collection<T> coll) {
    if (coll == null || coll.isEmpty())
        return null;
    return coll.iterator().next();
}

From source file:Main.java

public static <T> List<T> intersection(Collection<T> a, Collection<T> b) {
    ArrayList list = new ArrayList();
    Iterator i$ = a.iterator();

    while (i$.hasNext()) {
        Object element = i$.next();
        if (b.contains(element)) {
            list.add(element);// w w  w  .jav  a 2 s  . c  om
        }
    }

    return list;
}

From source file:Main.java

public static boolean containsInstance(Collection collection, Object element) {
    if (collection != null) {
        Iterator it = collection.iterator();

        while (it.hasNext()) {
            Object candidate = it.next();
            if (candidate == element) {
                return true;
            }//from www  .  j a va  2  s  .c o  m
        }
    }

    return false;
}

From source file:Main.java

/**
 * Returns a {@link Collection} containing <tt><i>a</i> - <i>b</i></tt>. The
 * cardinality of each element <i>e</i> in the returned {@link Collection}
 * will be the cardinality of <i>e</i> in <i>a</i> minus the cardinality of
 * <i>e</i> in <i>b</i>, or zero, whichever is greater.
 * /*from   w  w  w.j  a va2 s  . c o m*/
 * @see Collection#removeAll
 */
public static Collection subtract(final Collection a, final Collection b) {
    ArrayList list = new ArrayList(a);
    Iterator it = b.iterator();
    while (it.hasNext()) {
        list.remove(it.next());
    }
    return list;
}