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:net.sf.zekr.common.util.CollectionUtils.java

/**
 * @param col collection parameter to be returned as array
 * @return an array of <code>collection.eachElement.toString()</code>.
 *///from www  . j  av  a2 s  .  c o  m
public static String[] toStringArray(Collection<?> col) {
    String[] s = new String[col.size()];
    int i = 0;
    for (Iterator<?> iter = col.iterator(); iter.hasNext(); i++) {
        Object element = iter.next();
        s[i] = element.toString();
    }
    return s;
}

From source file:info.magnolia.cms.beans.config.MIMEMapping.java

/**
 * Cache all MIME types configured./*www.  j  a  v a2  s . c o  m*/
 */
private static void cacheContent(Collection mimeList) {
    Iterator iterator = mimeList.iterator();
    while (iterator.hasNext()) {
        Content c = (Content) iterator.next();
        try {
            MIMEMappingItem item = new MIMEMappingItem();
            item.ext = NodeDataUtil.getString(c, "extension", c.getName());//$NON-NLS-1$
            item.mime = c.getNodeData("mime-type").getString();//$NON-NLS-1$
            item.icon = NodeDataUtil.getString(c, "icon", "general.png");

            MIMEMapping.cachedContent.put(item.ext, item);
        } catch (Exception e) {
            log.error("Failed to cache MIMEMapping"); //$NON-NLS-1$
        }
    }
}

From source file:Main.java

/**
 * Determine whether the given Collection only contains a single unique object.
 *
 * @param collection the Collection to check
 * @return <code>true</code> if the collection contains a single reference or
 *         multiple references to the same instance, <code>false</code> else
 *//*  w w w .j  a  v  a 2 s . c o m*/
public static boolean hasUniqueObject(Collection collection) {
    if (isEmpty(collection)) {
        return false;
    }
    boolean hasCandidate = false;
    Object candidate = null;
    for (Iterator it = collection.iterator(); it.hasNext();) {
        Object elem = it.next();
        if (!hasCandidate) {
            hasCandidate = true;
            candidate = elem;
        } else if (candidate != elem) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Implementation of the OCL/*w  w w. j a  v a2  s . c om*/
 * <ul>
 * <li><tt>OrderedSet::at(index : Integer) : T</tt></li>
 * <li><tt>Sequence::at(index : Integer) : T</tt></li>
 * </ul>
 * operations.
 * 
 * @param self the source collection
 * @param index the 1-based (in OCL fashion) index
 * @return the object at the specified index of the source collection
 */
public static <E> E at(Collection<E> self, int index) {
    index = index - 1;

    if (index < 0 || index >= self.size()) {
        return null; // undefined
    }

    int curr = 0;
    for (Iterator<E> it = self.iterator(); it.hasNext();) {
        E object = it.next();
        if (curr++ == index) {
            return object;
        }
    }
    return null; // undefined
}

From source file:Main.java

/**
 * Returns <code>true</code> iff some element of <i>a</i> is also an element
 * of <i>b</i> (or, equivalently, if some element of <i>b</i> is also an
 * element of <i>a</i>). In other words, this method returns
 * <code>true</code> iff the {@link #intersection} of <i>a</i> and <i>b</i>
 * is not empty./*from ww  w .j av  a2  s.  co m*/
 * 
 * @since 2.1
 * @param a
 *            a non-<code>null</code> Collection
 * @param b
 *            a non-<code>null</code> Collection
 * @return <code>true</code> iff the intersection of <i>a</i> and <i>b</i>
 *         is non-empty
 * @see #intersection
 */
public static boolean containsAny(final Collection a, final Collection b) {
    // TO DO: we may be able to optimize this by ensuring either a or b
    // is the larger of the two Collections, but I'm not sure which.
    for (Iterator iter = a.iterator(); iter.hasNext();) {
        if (b.contains(iter.next())) {
            return true;
        }
    }
    return false;
}

From source file:StringUtil.java

/**
 * Determine if a String is contained in a String Collection
 * //  w  ww  . j  a  va2 s  .  c o m
 * @param stringCollection
 *        The collection of (String) to scan
 * @param value
 *        The value to look for
 * @return true if the string was found
 */
public static boolean contains(Collection stringCollection, String value) {
    if (stringCollection == null || value == null)
        return false;
    if (value.length() == 0)
        return false;
    for (Iterator i = stringCollection.iterator(); i.hasNext();) {
        Object o = i.next();
        if (!(o instanceof String))
            continue;
        if (value.equals((String) o))
            return true;
    }
    return false;
}

From source file:Main.java

/**
 * A version of {@link #zipMap(Object[], Object[])} that works with 
 * {@link Collection}s./*from  w  w  w  . j ava2  s.c  o m*/
 * 
 * @param keys Items that will be the keys in the resulting {@code Map}.
 * @param values Items that will be the values in the result {@code Map}.
 * 
 * @return A {@code Map} whose entries are of the form 
 * {@code keys[N], values[N]}.
 * 
 * @see #zipMap(Object[], Object[])
 */
public static <K, V> Map<K, V> zipMap(Collection<? extends K> keys, Collection<? extends V> values) {
    Map<K, V> zipped = new LinkedHashMap<>(keys.size());
    Iterator<? extends K> keyIterator = keys.iterator();
    Iterator<? extends V> valueIterator = values.iterator();
    while (keyIterator.hasNext() && valueIterator.hasNext()) {
        zipped.put(keyIterator.next(), valueIterator.next());
    }
    return zipped;
}

From source file:Main.java

public static Collection invokeForEach(Collection in, String methodName, Object[] args)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    Iterator<Object> it = in.iterator();

    Class[] argsClasses = new Class[args.length];
    for (int i = 0; i < args.length; i++)
        argsClasses[i] = args[i].getClass();

    LinkedList<Object> out = new LinkedList<Object>();

    while (it.hasNext()) {
        Object obj = it.next();//from   w ww .  j  a  va  2  s.  co m

        Method m = obj.getClass().getMethod(methodName, argsClasses);
        Object value2 = m.invoke(obj, args);
        out.add(value2);

    }
    return out;
}

From source file:StringUtil.java

/**
 * Determine if a String is contained in a String Collection, ignoring case
 * //from  ww  w. j  a v  a 2  s. co m
 * @param stringCollection
 *        The collection of (String) to scan
 * @param value
 *        The value to look for
 * @return true if the string was found
 */
public static boolean containsIgnoreCase(Collection stringCollection, String value) {
    if (stringCollection == null || value == null)
        return false;
    if (value.length() == 0)
        return false;
    for (Iterator i = stringCollection.iterator(); i.hasNext();) {
        Object o = i.next();
        if (!(o instanceof String))
            continue;
        if (value.equalsIgnoreCase((String) o))
            return true;
    }
    return false;
}

From source file:Main.java

/**
 * Performs a thorough null and empty check. This asserts that a given collection is null, empty or only consists of null values.
 *
 * @param col/*from   ww  w .j  av  a 2  s. c  o  m*/
 * @return asserts that a given collection is null, empty or only consists of null values.
 */
public static boolean isNullOrEmpty(Collection<?> col) {

    if ((col == null) || col.isEmpty())
        return true;
    else {

        try {

            for (Iterator<?> iterator = col.iterator(); iterator.hasNext();) {

                if (iterator.next() != null) {
                    return false;
                }
            }
        } catch (NullPointerException e) { // guard against null during concurrent modifications
            return false;
        }

        return true;
    }
}