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 first(Collection<? extends T> collection) {
    final T notFound = null;
    return isEmpty(collection) ? notFound : collection.iterator().next();
}

From source file:Main.java

public static long[] toArray(Collection<Long> collection) {
    long[] array = new long[collection.size()];
    Iterator<Long> it = collection.iterator();
    int i = 0;/*ww  w  .j  a  va  2 s . co m*/
    while (it.hasNext()) {
        array[i++] = it.next();
    }
    return array;
}

From source file:Main.java

public static boolean containsAny(Collection source, Collection candidates) {
    if (!isEmpty(source) && !isEmpty(candidates)) {
        Iterator it = candidates.iterator();

        do {/* www  . j  av  a  2 s  .  c o  m*/
            if (!it.hasNext()) {
                return false;
            }
        } while (!source.contains(it.next()));

        return true;
    } else {
        return false;
    }
}

From source file:Main.java

public static <T> T firstElement(Collection<T> c) {
    if (isEmpty(c)) {
        return null;
    }/*from  ww w.j  ava  2  s  .c  om*/
    return c.iterator().next();
}

From source file:Main.java

public static JComponent getComponentByName(Container parent, String name) {
    Collection components = getAllJComponents(parent);
    for (Iterator iterator = components.iterator(); iterator.hasNext();) {
        JComponent component = (JComponent) iterator.next();
        if (name.equals(component.getName()))
            return component;
    }//from w w w  .j  ava 2s .  c o  m
    return null;
}

From source file:Main.java

public static char[] asCharArray(Collection<Character> collection) {
    char[] array = new char[collection.size()];
    Iterator<Character> iterator = collection.iterator();
    int i = 0;//from  w  w  w .j a  v  a 2  s  .c  o  m
    while (iterator.hasNext()) {
        array[i++] = iterator.next();
    }
    return array;
}

From source file:Main.java

/**
 * Return the first item from a collection using the most efficient
 * method possible.//from  www.  j  a  v a2 s. c  om
 *
 * @param c The Collection.
 * @return the first element of a Collection.
 * @throws java.util.NoSuchElementException if the collection is empty.
 */
public static Object getFirstItem(Collection c) {
    if (c instanceof List) {
        return ((List) c).get(0);
    }
    return c.iterator().next();
}

From source file:Main.java

/**
 * add a collection of values to a collection.
 * //from ww w. j  a  v a 2s . c o m
 * @param collection
 *            the collection.
 * @param values
 *            the values.
 * @return boolean.
 * @since 0.1
 */
public static boolean addAllUnique(final Collection collection, final Collection values) {
    boolean changed = false;
    for (final Iterator iter = values.iterator(); iter.hasNext();) {
        changed = addUnique(collection, iter.next());
    }
    return changed;
}

From source file:Main.java

public static <T> T uniqueResult(final Collection<T> c) {
    switch (c.size()) {
    case 0:/*w ww .  j a v a 2 s. co  m*/
        return null;
    case 1:
        return c.iterator().next();
    default:
        throw new IllegalStateException("Unexpected number of elements in collection: " + c.size());
    }
}

From source file:Main.java

public static <E> E extractSingleton(Collection<E> collection) {
    if (collection != null && collection.size() == 1) {
        return collection.iterator().next();
    } else {//from www . j  a v  a  2 s .c om
        throw new IllegalArgumentException("Can extract singleton only when collection size == 1");
    }
}