List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
/** * Answer a set containing all elements that are both in source and in query * Delete these elements from source Creation date: (13.10.2002 16:23:00) * // w w w.ja va 2 s.co m * @return java.util.Set * @param source * java.util.Set * @param elements * java.util.Collection */ public static Set extractFrom(Set source, Collection query) { Set answer = new HashSet(); Iterator i = query.iterator(); while (i.hasNext()) { Object o = i.next(); if (source.remove(o)) { answer.add(o); } } return answer; }
From source file:Main.java
/** * Returns the number of occurrences of <i>obj</i> in <i>col</i>. *///w ww .ja va 2 s . c om public static int cardinality(Object obj, final Collection col) { int count = 0; Iterator it = col.iterator(); while (it.hasNext()) { Object elt = it.next(); if ((null == obj && null == elt) || obj.equals(elt)) { count++; } } return count; }
From source file:Main.java
public static <T> T firstElement(final Collection<T> values) { if (values != null && values.size() > 0) { return values.iterator().next(); }/*from w w w.java 2 s. c o m*/ return null; }
From source file:Main.java
/** Returns a new array or null if collection is empty */ public static <T> T[] toArray(Collection<T> c) { return c.isEmpty() ? null : toArray(c, c.iterator().next().getClass()); }
From source file:MainClass.java
protected static void print(String label, Collection c) { System.out.println("--------------" + label + "--------------"); Iterator it = c.iterator(); while (it.hasNext()) { System.out.println(it.next()); }//from w ww . j av a 2s. c o m }
From source file:Main.java
public static String getPrettyString(Collection<?> coll) { StringBuffer sb = new StringBuffer(); Iterator<?> it = coll.iterator(); while (it.hasNext()) { String s = it.next().toString(); sb.append(s + "\n"); }/*from w w w .ja v a2 s .c om*/ return sb.toString().trim(); }
From source file:Main.java
public static <T> T first(Collection collection) { if (size(collection) > 0) { return (T) collection.iterator().next(); }//from ww w. j a v a 2 s. c o m return null; }
From source file:Main.java
/** * Compares two collections returning the number of elements they have in common * // w w w . ja v a 2 s . c o m * @param <T> * @param one * @param two * @return */ public static <T> int collectionCompare(Collection<T> one, Collection<T> two) { int count = 0; Iterator<T> e = one.iterator(); while (e.hasNext()) { if (two.contains(e.next())) ++count; } return count; }
From source file:Main.java
/** * Returns a new {@link Collection} containing <tt><i>a</i> - <i>b</i></tt>. * The cardinality of each element <i>e</i> in the returned {@link Collection} * will be the cardinality of <i>e</i> in <i>a</i> minus the cardinality * of <i>e</i> in <i>b</i>, or zero, whichever is greater. * * @param a the collection to subtract from, must not be null * @param b the collection to subtract, must not be null * @return a new collection with the results * @see Collection#removeAll//from w w w . j a v a 2 s .co m */ public static Collection subtract(final Collection a, final Collection b) { ArrayList list = new ArrayList(a); for (Iterator it = b.iterator(); it.hasNext();) { list.remove(it.next()); } return list; }
From source file:Main.java
/** * Searches for and returns the first string which starts with the given * prefix. Removes the match from the collection. * * @param collection the collection./*from ww w.j ava2 s . co m*/ * @param prefix the string prefix. * @return a string, or null if no matches. */ public static String popStartsWith(Collection<String> collection, String prefix) { Iterator<String> iterator = collection.iterator(); while (iterator.hasNext()) { String element = iterator.next(); if (element != null && element.startsWith(prefix)) { iterator.remove(); return element; } } return null; }