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

/**
 * Returns a hashcode that is computed irrespective of the order of the collection.  This method should be used
 * to compute a hashcode of a collection field when <code>unorderedListEquals</code> or
 * <code>unorderedCollectionEquals</code> is used for that field in the object's equals method. 
 * /*from ww w. j av  a 2 s  . co m*/
 * @param c the <code>Collection</code>
 * @return the hashcode
 */
public static int unorderedCollectionHashCode(Collection<?> c) {
    if (c == null)
        return 0;

    int h = 0;
    Iterator<?> i = c.iterator();
    while (i.hasNext()) {
        Object o = i.next();
        if (o != null)
            h += o.hashCode();
    }
    return h;
}

From source file:doc.action.SelectedDocsUtils.java

private static JnuDocument getDocument(Collection presidents, String selectedPresident) {
    for (Iterator iter = presidents.iterator(); iter.hasNext();) {
        JnuDocument doc = (JnuDocument) iter.next();
        Integer documentId = doc.getId();
        if (documentId.toString().equals(selectedPresident)) {
            return doc;
        }//from w  w w.j  a va  2  s .c om
    }
    return null;
}

From source file:Main.java

/**
 * INTERNAL: Gets the first object in the collection. If the
 * collection does not contain any elements NoSuchElementException
 * is thrown.<p>/*from  w w  w. j a v a  2  s.  c om*/
 *
 * @since 1.3.4
 */
public static <T> T getFirstElement(Collection<T> coll) {
    if ((coll == null) || coll.isEmpty())
        throw new NoSuchElementException();
    return coll.iterator().next();
}

From source file:Main.java

public static <E> E first(Collection<E> collection) {

    if (isEmpty(collection)) {
        return null;
    }//  w ww.  ja va  2  s  .c o m

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

    if (iterator.hasNext()) {
        return iterator.next();
    }

    throw new IllegalStateException("Should not happen");
}

From source file:Main.java

public static <K> void addAllToArray(K[] destination, Collection<K> toBeAdded) {
    if (destination != null && toBeAdded != null) {

        int inx = 0;
        for (Iterator<K> iterator = toBeAdded.iterator(); iterator.hasNext(); inx++) {
            K k = iterator.next();//www .  j av  a2  s .  c o  m
            destination[inx] = k;
        }
    }
}

From source file:Main.java

/**
 * Returns an iterator on a collection even null.
 * @param collection Collection/*from  w w w.  ja va2 s  . c om*/
 * @return Iterator
 */
public static Iterator iterator(Collection collection) {
    if (collection == null) {
        return Collections.EMPTY_LIST.iterator();
    } else {
        return collection.iterator();
    }
}

From source file:Main.java

public static Object findObject(Collection<?> c, Object o, Comparator cmp) {
    if (c != null && o != null && cmp != null) {
        Iterator<?> it = c.iterator();
        while (it.hasNext()) {
            Object current = it.next();
            if (o.getClass().equals(current.getClass())) {
                if (cmp.compare(current, o) == 0) {
                    return current;
                }/*from www . java  2 s .c  o  m*/
            }
        }
    }
    return null;
}

From source file:Main.java

public static <T> T getFirst(Collection<T> collection) {
    if (isEmpty(collection)) {
        return null;
    }/*from   w  ww  .j  a  va 2s .c  o m*/

    return collection.iterator().next();
}

From source file:Main.java

public static <E> boolean isEmpty(Collection<E> collection) {
    if (collection == null || collection.isEmpty()) {
        return true;
    }/*from  w ww.  j  a v a 2  s.  c o m*/
    Iterator<E> it = collection.iterator();
    while (it.hasNext()) {
        E type = (E) it.next();
        if (type != null) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void UpperCaseItems(Collection collection) {

    //Basic iterator ... cannot modify collection using this method.
    Iterator iterator = collection.iterator();
    while (iterator.hasNext()) {
        Object object = iterator.next();
        if (object instanceof String) {
            System.out.println(((String) object).toUpperCase());
            //object = ((String) object).toUpperCase();  doesn't actually change the underlying object.
        }/* w  w w.ja va2 s .  com*/
    }

    //old school for loop ... can modify collection. This is not a structural change to the collection, just a change to the members. Structural collection changes in a loop is not recommended.
    if (collection instanceof ArrayList) {
        for (int i = 0; i < collection.size(); i++) {
            Object object = ((ArrayList) collection).get(i);
            String item = ((String) object);
            ((ArrayList) collection).set(i, item.toUpperCase());
        }
    }
}