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:com.springsource.hq.plugin.tcserver.serverconfig.ValidationUtils.java

/**
 * Performs validation of the given <code>Collection</code> of <code>Validator</code> instances. Each
 * <code>Validator</code> is treated as a self-validating domain object, i.e. the following is called upon each
 * <code>Validator</code>: <code>validator.validate(validator, errors)</code>. The given <code>identifier</code> is
 * used when pushing the path on the given <code>errors</code>, e.g. an identifier of 'foo' will result in
 * <code>foo[i]</code> being pushed, where i is the index of the validator in the given collection. The given
 * <code>errors</code> will be used to record any errors that are found.
 * /*w  w w  . j a v a 2s  .  c  om*/
 * @see Validator#validate(Object, Errors)
 * 
 * @param selfValidatingItems The self-validating items to validate
 * @param identifier The identifier
 * @param errors Passed to each validator and to be used to record any errors
 */
public static void validateCollection(Collection<? extends Validator> selfValidatingItems, String identifier,
        Errors errors) {
    Iterator<? extends Validator> iterator = selfValidatingItems.iterator();
    int index = 0;
    while (iterator.hasNext()) {
        errors.pushNestedPath(String.format("%s[%d]", identifier, index++));
        Validator selfValidating = iterator.next();
        selfValidating.validate(selfValidating, errors);
        errors.popNestedPath();
    }
}

From source file:Main.java

/**
 * Get a child element with the specified localName. Assumption is that it
 * is in the same namespace as the specified element.
 * //from  www.  java  2  s  . c  om
 * @param element
 * @param localName
 * @return
 */
public static Element getElement(Element element, String localName) {
    Collection<Element> elements = getElementsByTagName(element, localName);
    if (elements.isEmpty()) {
        return null;
    } else {
        return elements.iterator().next();
    }
}

From source file:Main.java

/**
 * Convert collection to array. This is a bit cleaner version of
 * {@link Collection#toArray(Object[])}.
 *//*from w  w  w.j  a  v a 2 s  . c o  m*/
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<? extends T> collection, Class<T> type) {
    T[] result = (T[]) java.lang.reflect.Array.newInstance(type, collection.size());

    Iterator<? extends T> it = collection.iterator();
    for (int i = 0; i < collection.size(); i++) {
        result[i] = it.next();
    }

    return result;
}

From source file:com.enonic.cms.domain.structure.menuitem.section.SectionContentKey.java

public static String convertToCommaSeparatedString(Collection<SectionContentKey> keys) {
    StringBuffer s = new StringBuffer();
    for (Iterator<SectionContentKey> it = keys.iterator(); it.hasNext();) {
        SectionContentKey menuItemKey = it.next();
        s.append(menuItemKey);//from   ww w .j a va  2 s .c o m
        if (it.hasNext()) {
            s.append(",");
        }
    }
    return s.toString();
}

From source file:CollectionUtils.java

/**
 * Return the first element in '<code>candidates</code>' that is contained in
 * '<code>source</code>'. If no element in '<code>candidates</code>' is present in
 * '<code>source</code>' returns <code>null</code>. Iteration order is
 * {@link Collection} implementation specific.
 * @param source the source Collection//from  w  ww  . ja va  2  s.co m
 * @param candidates the candidates to search for
 * @return the first present object, or <code>null</code> if not found
 */
public static Object findFirstMatch(Collection source, Collection candidates) {
    if (isEmpty(source) || isEmpty(candidates)) {
        return null;
    }
    for (Iterator it = candidates.iterator(); it.hasNext();) {
        Object candidate = it.next();
        if (source.contains(candidate)) {
            return candidate;
        }
    }
    return null;
}

From source file:Main.java

public static <T> T first(Collection<T> c) {
    if (isEmpty(c))
        return null;

    if (c instanceof List)
        return (T) ((List<T>) c).get(0);

    Iterator<T> iter = c.iterator();
    if (iter.hasNext()) {
        return iter.next();
    }// w  ww.j a va 2 s  .c  o m
    return null;
}

From source file:Main.java

/**
 * Returns <code>true</code> iff at least one element is in both collections.
 * <p/>/*from   w  w w .  j a  v  a2s.  c  o  m*/
 * In other words, this method returns <code>true</code> iff the
 * {@link #intersection} of <i>coll1</i> and <i>coll2</i> is not empty.
 *
 * @param coll1 the first collection, must not be null
 * @param coll2 the first collection, must not be null
 * @return <code>true</code> iff the intersection of the collections is non-empty
 * @see #intersection
 */
public static boolean containsAny(final Collection coll1, final Collection coll2) {
    if (coll1.size() < coll2.size()) {
        for (Iterator it = coll1.iterator(); it.hasNext();) {
            if (coll2.contains(it.next()))
                return true;
        }
    } else {
        for (Iterator it = coll2.iterator(); it.hasNext();) {
            if (coll1.contains(it.next()))
                return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Extracts certificate from APK//  w ww.  j  a  va  2 s  .  c  o  m
 * 
 * @param jar
 * @param entry
 *            rsa or dsa jar item
 * @return
 * @throws IOException
 *             I/O problem to read jar
 * @throws CertificateException
 *             certificate has problems
 */
private static List<Certificate> extractCertificate(JarFile jar, JarEntry entry)
        throws IOException, CertificateException {
    List<Certificate> certList = new ArrayList<Certificate>();
    InputStream inStream = null;
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        inStream = jar.getInputStream(entry);
        Collection<? extends Certificate> c = cf.generateCertificates(inStream);
        Iterator<? extends Certificate> i = c.iterator();
        while (i.hasNext()) {
            Certificate cert = i.next();
            certList.add(cert);
        }
    } finally {
        if (inStream != null) {
            inStream.close();
        }
    }
    return certList;
}

From source file:CollectionUtilities.java

public static Collection reverse(Collection collection) {
    LinkedList newCollection = new LinkedList();
    Iterator i = collection.iterator();
    while (i.hasNext()) {
        newCollection.addFirst(i.next());
    }/*w w w  .j av  a2s  . co  m*/
    return newCollection;
}

From source file:Main.java

static public <E, T> void writeTo(Collection<E> c, String opener, String separator, String closer,
        Writer writer) throws IOException {

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

    if (!iterator.hasNext()) {
        writer.write(opener);//  w w  w .  java 2s.co  m
        writer.write(closer);
        return;
    }

    writer.write(opener);
    do {
        E element = iterator.next();
        writer.write(element.toString());

        if (!iterator.hasNext())
            break;
        writer.write(separator);
    } while (true);
    writer.write(closer);
}