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.enonic.cms.domain.structure.page.template.PageTemplateKey.java

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

From source file:Main.java

public static String recursiveDeepToString(final Collection<?> collection) {
    final StringBuilder sb = new StringBuilder(3 * collection.size());
    sb.append("[");
    final Iterator<?> iter = collection.iterator();
    if (iter.hasNext()) {
        final Object item = iter.next();
        if (item instanceof Collection) {
            final Collection<?> subCollection = (Collection<?>) item;
            sb.append(recursiveDeepToString(subCollection));
        } else/*from ww w. j  a v  a  2  s .  c o  m*/
            sb.append(item);
    }
    while (iter.hasNext()) {
        final Object item = iter.next();
        sb.append(", ");
        if (item instanceof Collection) {
            final Collection<?> subCollection = (Collection<?>) item;
            sb.append(recursiveDeepToString(subCollection));
        } else
            sb.append(item);
    }
    sb.append("]");
    return sb.toString();
}

From source file:Main.java

/**
 * makes sense only if iteration order deterministic!
 *///from  w  ww . j  av  a  2 s. c  om
private static <T> T getElement(final boolean remove, final int index, final Collection<T> coll) {
    if (index >= coll.size())
        throw new IndexOutOfBoundsException(index + " >= " + coll.size());

    final Iterator<T> it = coll.iterator();
    int i = 0;

    while (i++ < index)
        it.next();

    final T elem = it.next();
    if (remove)
        it.remove();
    return elem;
}

From source file:Main.java

/**
 * Returns <tt>true</tt> iff <i>a</i> is a sub-collection of <i>b</i>, that
 * is, iff the cardinality of <i>e</i> in <i>a</i> is less than or equal to
 * the cardinality of <i>e</i> in <i>b</i>, for each element <i>e</i> in
 * <i>a</i>.//from   w ww  .j  a  v  a  2s .  co  m
 * 
 * @see #isProperSubCollection
 * @see Collection#containsAll
 */
public static boolean isSubCollection(final Collection a, final Collection b) {
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Iterator it = a.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        if (getFreq(obj, mapa) > getFreq(obj, mapb)) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Removes null objects from the Collection.
 * @param p_collection a Collection/*from   w  ww.  j  a  v  a  2 s .  c  o m*/
 * @returns true if one or more null objects were removed.
 */
public static boolean removeNulls(Collection p_collection) {
    if (p_collection == null) {
        return false;
    }
    boolean removed = false;
    Iterator it = p_collection.iterator();
    while (it.hasNext()) {
        Object o = it.next();
        if (o == null) {
            removed = true;
            it.remove();
        }
    }
    return removed;
}

From source file:Main.java

public static <T> List<T> intersection(Collection<? extends Collection<T>> availableValuesByDescriptor) {
    List<T> result = new ArrayList<T>();
    Iterator<? extends Collection<T>> iterator = availableValuesByDescriptor.iterator();
    if (iterator.hasNext()) {
        Collection<T> firstSet = iterator.next();
        result.addAll(firstSet);/*from   w w w. j  a  v a  2s  . c o  m*/
        while (iterator.hasNext()) {
            Collection<T> next = iterator.next();
            result.retainAll(next);
        }
    }
    return result;

}

From source file:com.partydj.search.LuceneSearchProvider.java

private static String getFirst(Map<String, Collection<String>> map, String key) {
    Collection<String> c = map.get(key);
    if (c != null && c.size() > 0) {
        return c.iterator().next();
    }// www .  j  a  v a2  s .  c  o  m
    return null;
}

From source file:Main.java

public static Object findSingleObject(Collection c) {
    if (c == null || c.isEmpty())
        return null;
    if (c.size() > 1)
        throw new IllegalStateException("found more than one object when single object requested");
    return c.iterator().next();
}

From source file:Main.java

/**
 * Returns <code>true</code> iff at least one element is in both collections.
 * <p>/*from ww w .  j  av a 2 s . 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
 * @since 2.1
 * @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:de.tudarmstadt.ukp.wikipedia.util.StringUtils.java

/**
 * Joins the elements of a collection into a string.
 * @param c The collection which elements should be joined.
 * @param delimiter String that is introduced between two joined elements.
 * @return The joined string.//from w  w  w  . j  ava 2  s.  co  m
 */
public static String join(Collection c, String delimiter) {
    StringBuffer buffer = new StringBuffer();
    Iterator iter = c.iterator();
    while (iter.hasNext()) {
        buffer.append(iter.next());
        if (iter.hasNext()) {
            buffer.append(delimiter);
        }
    }
    return buffer.toString();
}