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 all objects of given type and its subtypes
 * @param clazz The class you want to return all of the instances of.
 * @param objects The collection you'd like to search.
 * @return A List of all of the instances of a specific Class found in a Collection.
 *//*w w  w  .j a v a  2  s  .co m*/
public static List getAllInstances(Class clazz, Collection objects) {
    List allInstances = new ArrayList();
    if (objects != null && clazz != null) {
        Iterator objectsIterator = objects.iterator();
        while (objectsIterator.hasNext()) {
            Object instance = objectsIterator.next();
            if (instance != null) {
                if (clazz.isAssignableFrom(instance.getClass())) {
                    allInstances.add(instance);
                }
            }
        }
    }
    return allInstances;
}

From source file:Main.java

public static <T> String toString(Collection<T> c, String prefix, String sep, String suffix) {
    if (c == null || c.size() == 0)
        return prefix + suffix;
    Iterator<T> it = c.iterator();
    String result = prefix + it.next();
    while (it.hasNext())
        result += sep + it.next();/*w  w w.  j  av a 2 s  .  c  o m*/
    return result + suffix;
}

From source file:Main.java

/**
 * clone all elements//from ww  w  .  j  ava 2  s. co m
 * 
 * @param inputCollection
 *            the collection to get the input from, may not be null
 * @return all elements (new list)
 * @throws NullPointerException
 *             if the input collection is null
 */
public static <E> Collection<E> cloneEx(Collection<E> inputCollection) {
    final ArrayList<E> outputCollection = new ArrayList<E>(inputCollection.size());
    for (Iterator<E> iter = inputCollection.iterator(); iter.hasNext();) {
        outputCollection.add(iter.next());
    }
    return outputCollection;
}

From source file:com.emarsys.predict.StringUtil.java

/**
 * Constructs a new String from the elements of input list separated elements with delimiter.
 *
 * @param l         items for the construction
 * @param delimiter the delimiter//from   w w  w.j  av a2  s  .c  o m
 * @return the constructed string
 */
static String toStringWithDelimiter(Collection<?> l, String delimiter) {
    String ret = "";
    if (l != null && !l.isEmpty()) {
        Iterator<?> i = l.iterator();
        while (i.hasNext()) {
            ret += i.next() + delimiter;
        }
        ret = ret.substring(0, ret.length() - delimiter.length());
    }
    return ret;
}

From source file:info.magnolia.cms.gui.controlx.search.SimpleSearchUtil.java

public static SearchQueryExpression chainExpressions(Collection expressions, String operator) {
    SearchQueryExpression expr = null;/*from w ww.  ja  v  a  2s .c  om*/
    for (Iterator iter = expressions.iterator(); iter.hasNext();) {
        SearchQueryExpression newExpr = (SearchQueryExpression) iter.next();
        if (expr == null) {
            expr = newExpr;
        } else {
            SearchQueryOperator opt = new SearchQueryOperator(operator);
            opt.setleft(newExpr);
            opt.setRight(expr);
            expr = opt;
        }
    }
    return expr;
}

From source file:Main.java

public static Collection or(Collection collection1, Collection collection2) {
    Vector completeList = new Vector(collection1);

    if (collection2 == null)
        return collection1;
    Iterator i = collection2.iterator();
    // Do the long way tro make sure no dups
    while (i.hasNext()) {
        Object addlItem = i.next();
        if (collection1.contains(addlItem) == false) {
            completeList.addElement(addlItem);
        }//from w ww .  j a  v a2 s .c  o  m
    }
    return completeList;
}

From source file:com.jklas.sample.petclinic.util.EntityUtils.java

/**
 * Look up the entity of the given class with the given id
 * in the given collection./* www  . jav  a 2s.  c o m*/
 * @param entities the collection to search
 * @param entityClass the entity class to look up
 * @param entityId the entity id to look up
 * @return the found entity
 * @throws ObjectRetrievalFailureException if the entity was not found
 */
public static Entity getById(Collection entities, Class entityClass, int entityId)
        throws ObjectRetrievalFailureException {

    for (Iterator it = entities.iterator(); it.hasNext();) {
        Entity entity = (Entity) it.next();
        if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) {
            return entity;
        }
    }
    throw new ObjectRetrievalFailureException(entityClass, new Integer(entityId));
}

From source file:Main.java

public static <T> T getAt(Collection<T> col, int index) {
    if (col instanceof List)
        return ((List<T>) col).get(index);
    int i;/*w  w  w  . j a  v a2s . c om*/
    Iterator<T> it;
    for (i = 0, it = col.iterator(); i < index && it.hasNext(); it.next())
        ;
    return it.hasNext() ? it.next() : null;
}

From source file:doc.action.SelectedDocsUtils.java

public static Collection getSelectedDocuments(Collection documents, Collection selectedDocumentsIds) {
    Collection result = new ArrayList();

    for (Iterator iter = selectedDocumentsIds.iterator(); iter.hasNext();) {
        String selectedPresident = (String) iter.next();
        result.add(getDocument(documents, selectedPresident));
    }//from   w  w w  .  j a va2 s .c om

    return result;
}

From source file:Main.java

public static String join(Collection<?> items, String delimiter) {
    if (items == null || items.isEmpty()) {
        return "";
    }//from w  w w.ja  v a  2 s.co  m

    final Iterator<?> iter = items.iterator();
    final StringBuilder buffer = new StringBuilder(iter.next().toString());

    while (iter.hasNext()) {
        buffer.append(delimiter).append(iter.next());
    }

    return buffer.toString();
}