List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
/** * Transfer up to 'maxElems' elements from the source to the destination. *///from w w w . j a va2s . c om public static void transfer(Collection source, Collection dest, int maxElems) { int j = 0; for (Iterator i = source.iterator(); i.hasNext() && j < maxElems; j++) { dest.add(i.next()); i.remove(); } }
From source file:Main.java
public static <E> void print(Collection<E> collections) { StringBuilder builder = new StringBuilder(); for (Iterator<E> iterator = collections.iterator(); iterator.hasNext();) { E e = iterator.next();/*w w w .j a va 2 s.co m*/ if (e == null) { builder.append(",null"); } else { builder.append("," + e.toString()); } } System.out.println(builder.substring(1, builder.length())); }
From source file:Main.java
/** * Answer the junction between a and b. Answer is of the same type as a. * /*from www .j a v a 2s.co m*/ * @param a * @param b * @return */ public static Collection junction(Collection a, Collection b) { Collection answer = createCollection(a); for (Iterator i = a.iterator(); i.hasNext();) { Object oa = i.next(); for (Iterator k = b.iterator(); k.hasNext();) { Object ob = k.next(); if (oa.equals(ob)) { answer.add(oa); } } } return answer; }
From source file:Main.java
/** * Prints each object in the collection with commas between them. * Nulls will print as "null". A null collection will cause NPE. *//*from w ww .j ava 2s . c o m*/ public static String collectionToString(Collection col) { StringBuffer outputString = new StringBuffer(100); boolean firstItem = true; Iterator it = col.iterator(); while (it.hasNext()) { if (!firstItem) outputString.append(", "); outputString.append(it.next()); firstItem = false; } return outputString.toString(); }
From source file:Main.java
/** * An flexible alternative for converting a Collection to a String. * * @param c The Collection to convert to a String * @param start The String to place at the beginning of the returned String * @param separator The String to place in between elements of the Collection c. * @param end The String to place at the end of the returned String * @return A String which starts with 'start', followed by the elements in the Collection c separated by 'separator', * ending with 'end'./*w w w.j a va2 s . c om*/ */ @SuppressWarnings("rawtypes") public static String toString(Collection c, String start, String separator, String end) { Iterator i = c.iterator(); StringBuilder myString = new StringBuilder(); if (start != null) { myString.append(start); } boolean first = true; while (i.hasNext()) { if (!first) { myString.append(separator); } myString.append(i.next().toString()); first = false; } if (end != null) { myString.append(end); } return myString.toString(); }
From source file:Main.java
public static <T> Collection<T> reverse(final Collection<T> collection) { final LinkedList<T> newCollection = new LinkedList<>(); final Iterator<T> i = collection.iterator(); while (i.hasNext()) { newCollection.addFirst(i.next()); }/*from w w w .jav a 2 s . c o m*/ return newCollection; }
From source file:Main.java
@SuppressWarnings({ "rawtypes", "unchecked" }) public static Map getCardinalityMap(final Collection coll) { final Map count = new HashMap(); for (final Iterator it = coll.iterator(); it.hasNext();) { final Object obj = it.next(); final Integer c = (Integer) (count.get(obj)); if (c == null) { count.put(obj, INTEGER_ONE); } else {//from w w w . j ava 2 s. c o m count.put(obj, new Integer(c.intValue() + 1)); } } return count; }
From source file:Main.java
public static StringBuilder join(CharSequence separator, Collection<?> col) { final StringBuilder result = new StringBuilder(); if (col != null) { for (Iterator<?> it = col.iterator(); it.hasNext();) { result.append(it.next());// w w w .j av a2 s . c om if (it.hasNext()) { result.append(separator); } } } return result; }
From source file:Main.java
public static <T> T first(Collection<T> collection) { if (collection == null || collection.isEmpty()) { return null; }// w ww . j av a 2 s. co m return collection.iterator().next(); }
From source file:Main.java
public static <T> Set<String> toStringSet(Collection<T> a_values) /* */ {/* ww w.j a v a 2s .com*/ /* 443 */Set values = new HashSet(); /* */ /* 445 */for (Iterator i$ = a_values.iterator(); i$.hasNext();) { Object value = i$.next(); /* */ /* 447 */values.add(value.toString()); /* */} /* */ /* 450 */return values; /* */}