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 String join(Collection<String> collection, String delimiter) {
    StringBuffer buffer = new StringBuffer();
    Iterator<String> iter = collection.iterator();
    while (iter.hasNext()) {
        buffer.append(iter.next());//from   www  . j  a va  2s .  c  o  m
        if (iter.hasNext()) {
            buffer.append(delimiter);
        }
    }
    return buffer.toString();
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map getCardinalityMap(final Collection coll) {
    Map count = new HashMap();
    for (Iterator it = coll.iterator(); it.hasNext();) {
        Object obj = it.next();//from w ww.  ja va 2 s  .c  o m
        Integer c = (Integer) (count.get(obj));
        if (c == null) {
            count.put(obj, INTEGER_ONE);
        } else {
            count.put(obj, new Integer(c.intValue() + 1));
        }
    }
    return count;
}

From source file:Main.java

public static String join(Collection<?> s, String delimiter) {
    StringBuilder builder = new StringBuilder();
    Iterator<?> iter = s.iterator();
    while (iter.hasNext()) {
        builder.append(iter.next());/*from  www  . j ava2s  . c  o m*/
        if (iter.hasNext()) {
            builder.append(delimiter);
        }
    }
    return builder.toString();
}

From source file:Main.java

public static <T> T getFirstElement(Collection<T> collection) {
    if (collection == null || collection.isEmpty()) {
        return null;
    }/*from w  w  w.  j av a 2 s  .co m*/

    return collection.iterator().next();
}

From source file:Main.java

public static <T extends Object> T getFirst(Collection<T> items, T otherwise) {
    if (items == null || items.size() == 0)
        return otherwise;
    return items.iterator().next();
}

From source file:Main.java

/**
 * Subtract objects in collection b from collection a. The string value
 * of the object is used for comparisons.
 * //from  w w w.  j  av a 2s .c om
 * @param a a collection that will have members removed
 * @param b a collection whose objects will be removed from a
 * @return a new collection that contains the remaining objects in a
 */
public static Collection subtractByString(Collection a, Collection b) {
    Collection retColl = new ArrayList();
    Object obj = null;
    Iterator it = a.iterator();
    while (it.hasNext()) {
        obj = it.next();
        if (!b.contains(obj)) {
            retColl.add(obj);
        }
    }
    return retColl;
}

From source file:Main.java

/**
 * Implementation of the OCL//from ww  w .  j ava2s.  com
 * <ul>
 * <li><tt>OrderedSet::first() : T</tt></li>
 * <li><tt>Sequence::first() : T</tt></li>
 * </ul>
 * operations.
 * 
 * @param self the source collection
 * @return the first object of the source collection
 */
public static <E> E first(Collection<E> self) {
    if (self.isEmpty()) {
        return null; // undefined
    }
    return self.iterator().next();
}

From source file:Main.java

/**
 * Returns a String representation of the elements delimited with the specified delimiter 
 * //from   w  ww.j a va  2  s .  co  m
 * @param elements the elements to build the String out of
 * @param delimiter the specified delimiter
 * @return the delimited String
 */
public static <T> String asDelimitedString(Collection<T> elements, String delimiter) {
    StringBuilder sb = new StringBuilder();
    Iterator<T> iterator = elements.iterator();
    while (iterator.hasNext()) {
        sb.append(iterator.next().toString());
        sb.append(iterator.hasNext() ? delimiter : EMPTY_STRING);
    }
    return sb.toString();
}

From source file:Main.java

public static <T extends Comparable<T>> int compare(Collection<T> lhs, Collection<T> rhs,
        Comparator<T> comparator) {
    int cmp;//from  w  w  w.  ja  v  a 2s .  co  m
    Iterator<T> lit = lhs.iterator();
    Iterator<T> rit = rhs.iterator();
    while (lit.hasNext() && rit.hasNext()) {
        cmp = comparator.compare(lit.next(), rit.next());
        if (cmp != 0) {
            return cmp;
        }
    }
    return lhs.size() - rhs.size();
}

From source file:Main.java

public static <T> Set<T> intersection(Collection<? extends T> c1, Collection<? extends T> c2) {
    Set<T> result = new HashSet<T>();
    for (Iterator<? extends T> it = c1.iterator(); it.hasNext();) {
        T element = it.next();//w w  w. j  ava 2  s  .c  o m
        if (c2.contains(element))
            result.add(element);
    }
    return result;
}