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 containsInstance(Collection collection, Object element) {
    if (collection != null) {
        Iterator i$ = collection.iterator();

        while (i$.hasNext()) {
            Object candidate = i$.next();
            if (candidate == element) {
                return true;
            }//from w ww.  j a  va 2 s . c om
        }
    }

    return false;
}

From source file:Main.java

public static Iterator pairs(Collection l) {
    List x = new LinkedList();
    Object prev = null;/*  w w w.j a  v a2s . co  m*/
    for (Iterator i = l.iterator(); i.hasNext();) {
        Object curr = i.next();
        if (prev != null)
            x.add(new Object[] { prev, curr });
        prev = curr;
    }
    return x.iterator();
}

From source file:Main.java

public static <T> T[] convertArrayToList(Collection<T> list) {
    return (T[]) (list.size() > 0
            ? list.toArray((Object[]) Array.newInstance(list.iterator().next().getClass(), list.size()))
            : null);/*from w  w  w .  j av a 2 s  .c  o  m*/
}

From source file:Util.java

public static String dumpToString(Collection c, String separator) {
    String retval = "";
    for (Iterator it = c.iterator(); it.hasNext();) {
        retval += String.valueOf(it.next());
        retval += separator;//from www. j  a v  a 2 s  .c o  m
    }
    return retval;
}

From source file:Main.java

public static String toString(Collection coll, String delim) {
    StringBuffer sb = new StringBuffer();
    for (Iterator it = coll.iterator(); it.hasNext();) {
        Object obj = it.next();//from  w w w . j av a2  s .  c  o m
        sb.append(String.valueOf(obj));
        if (it.hasNext()) {
            sb.append(delim);
        }
    }
    return sb.toString();
}

From source file:Main.java

public static <E> boolean contains(Collection<E> c, E item, BiPredicate<? super E, ? super E> equalTester) {
    Iterator<E> iter = c.iterator();
    while (iter.hasNext()) {
        if (equalTester.test(iter.next(), item)) {
            return true;
        }/*  w ww.  j  a  v  a  2 s. c om*/
    }
    return false;
}

From source file:Main.java

public static <T> List<T> ToList(Collection<List<T>> values) {
    List<T> alllist = new ArrayList<T>();
    Iterator<List<T>> iterator = values.iterator();
    List<T> list = null;//from  w w w.j  ava 2  s . c o m
    while (iterator.hasNext()) {
        list = (List<T>) iterator.next();
        alllist.addAll(list);
    }
    return alllist;
}

From source file:Main.java

/**
 * Filter the collection by applying a Predicate to each element. If the
 * predicate returns false, remove the element. If the input collection or
 * predicate is null, there is no change made.
 *
 * @param input/*ww  w  .  j a va  2s .c  om*/
 * @param predicate
 * @param <V>
 * @return
 */
public static <V> void filter(Collection<V> input, Predicate<V> predicate) {

    Iterator<V> iterator = input.iterator();

    while (iterator.hasNext()) {

        V value = iterator.next();

        if (!predicate.test(value)) {
            iterator.remove();
        }

    }

}

From source file:Main.java

/** Returns true if all elements in the collection are the same */
public static <T> boolean allEqual(Collection<T> elements) {
    if (elements.isEmpty())
        return false;
    Iterator<T> it = elements.iterator();
    T first = it.next();/*w ww .j  a va2 s  . c o  m*/
    while (it.hasNext()) {
        T el = it.next();
        if (!el.equals(first))
            return false;
    }
    return true;
}

From source file:Main.java

public static List<String> removeItemFromCollectionByReturningNewList(Collection<String> collection,
        String elementToBeRemoved) {

    Iterator iterator = collection.iterator();

    List<String> filteredCollection = new ArrayList<>();

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

        if (!element.equals(elementToBeRemoved)) {
            filteredCollection.add(element);
        }//w w w.  ja v  a  2s  .  co  m
    }

    return filteredCollection;
}