List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
public static <T> void move(Collection<T> fromCollection, Collection<T> toCollection) { for (Iterator<T> i = fromCollection.iterator(); i.hasNext();) { T element = i.next();/*from ww w .j a v a 2 s . c o m*/ i.remove(); toCollection.add(element); } }
From source file:Main.java
public static String dumpToString(Collection c, String separator) { String retval = ""; for (Iterator it = c.iterator(); it.hasNext();) { retval += it.next().toString();//from w w w . j av a 2s .c om retval += separator; } return retval; }
From source file:Main.java
/** * Brute force, for when HashSet and TreeSet won't work (e.g. #hashCode * implementation isn't appropriate). The original Collection is not * modified./*from www . j av a 2 s .c om*/ */ public static Collection removeDuplicates(Collection original) { ArrayList result = new ArrayList(); for (Iterator i = original.iterator(); i.hasNext();) { Object item = i.next(); if (!result.contains(item)) { result.add(item); } } return result; }
From source file:Main.java
/** * Get the first item in a list. If there is no such item, return * null./* w w w .ja v a 2 s. c om*/ */ public static <T> T first(Collection<T> list) { if (list == null || list.isEmpty()) return null; return list.iterator().next(); }
From source file:Main.java
public static String join(Collection<String> strings, String separator) { Iterator<String> iter = strings.iterator(); StringBuilder sb = new StringBuilder(); if (iter.hasNext()) { sb.append(iter.next());//from www . j a v a 2 s.c om while (iter.hasNext()) { sb.append(separator).append(iter.next()); } } return sb.toString(); }
From source file:Main.java
static String getContents(Collection c) { StringBuilder sb = new StringBuilder(); for (Iterator it = c.iterator(); it.hasNext();) { sb.append(it.next());//from www . ja v a2s . c o m if (it.hasNext()) { sb.append(", "); } } return sb.toString(); }
From source file:Main.java
public static <T> List<T> ToList2(Collection<T> values) { List<T> alllist = new ArrayList<T>(); Iterator<T> iterator = values.iterator(); T obj = null;/*from w ww .j av a 2s . c o m*/ while (iterator.hasNext()) { obj = iterator.next(); alllist.add(obj); } return alllist; }
From source file:Main.java
public static <T extends Comparable<T>> int compare(Collection<T> lhs, Collection<T> rhs) { int cmp;// www .ja va2 s. co m Iterator<T> lit = lhs.iterator(); Iterator<T> rit = rhs.iterator(); while (lit.hasNext() && rit.hasNext()) { cmp = lit.next().compareTo(rit.next()); if (cmp != 0) { return cmp; } } return lhs.size() - rhs.size(); }
From source file:Main.java
/** * Creates new collection that does not contain nulls. Does not change input * collection. It uses raw collection - not parameterized. * {@link SuppressWarnings} annotation is added to avoid IDE to report * rawtypes and unchecked warnings.//from w ww .j a v a 2 s. com * * @param c * raw collection to create cleaned collection from. * @return collection without nulls */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Collection toCleanedRaw(Collection c) { ArrayList cleaned = new ArrayList(); Iterator iterator = c.iterator(); while (iterator.hasNext()) { Object e = iterator.next(); if (e != null) { cleaned.add(e); } } return cleaned; }
From source file:Main.java
public static <T extends Object> boolean isEqual(Collection<? extends T> coll, Comparator<? super T> comp) { Iterator<? extends T> i = coll.iterator(); T first = i.next();//www . j av a2 s. co m while (i.hasNext()) { T next = i.next(); if (comp.compare(first, next) < 0) { return false; } } return true; }