List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
@Nullable public static Class getComponentClass(Collection<?> list) { return isEmpty(list) ? null : list.iterator().next().getClass(); }
From source file:Main.java
/** * Removes nulls from provided collection. * /*from w w w .ja v a 2 s. co m*/ * @param c * to remove nulls from * @return number of removed elements */ @SuppressWarnings("rawtypes") public static int removeNulls(Collection c) { int removed = 0; Iterator iterator = c.iterator(); while (iterator.hasNext()) { if (iterator.next() == null) { iterator.remove(); removed++; } } return removed; }
From source file:Main.java
public static <T> T first(Collection<T> collection) { if (isNotEmpty(collection)) { return collection.iterator().next(); }//from w ww . j av a 2 s .c om return null; }
From source file:Main.java
public static void writeToWriter(Collection s, Writer out) throws IOException { writeToWriter(s.iterator(), out); }
From source file:Main.java
public static long getSum(Collection<? extends Number> collection) { Iterator<? extends Number> iterator = collection.iterator(); long sum = 0; while (iterator.hasNext()) { sum += iterator.next().longValue(); }/*from w w w.j a v a 2 s . c o m*/ return sum; }
From source file:Main.java
public static <T> T find(Collection<T> collection, Predicate<T> whatToFind) { for (Iterator<T> iter = collection.iterator(); iter.hasNext();) { T item = iter.next();// w w w .ja v a2 s . c o m if (whatToFind.apply(item)) { return item; } } return null; }
From source file:Main.java
/** * Gets one element from the provided Collection. If no element is present, * an Exception is thrown. No guarantee is made as to which element is * returned from time to time.// w w w. j av a 2s. c om * * @param <T> Collection type * @param collection to use * @return one element from the provided Collection * @throws NoSuchElementException if no elements are present in the * Collection */ public static <T> T getAnyFrom(Collection<T> collection) { final Iterator<T> iterator = collection.iterator(); if (iterator.hasNext()) { return iterator.next(); } throw new NoSuchElementException("No value present"); }
From source file:Main.java
public static <T> List<T> subtract(Collection<T> a, Collection<T> b) { ArrayList list = new ArrayList(a); Iterator i$ = b.iterator(); while (i$.hasNext()) { Object element = i$.next(); list.remove(element);//from w w w. ja v a2 s.c om } return list; }
From source file:Main.java
/** * INTERNAL: Gets the first object in the collection. If the * collection is empty, null is returned. */// w w w.j a v a2 s . c o m public static <T> T getFirst(Collection<T> coll) { return (coll == null) || coll.isEmpty() ? null : coll.iterator().next(); }
From source file:Main.java
/** * Return first duplicate value that is not null, or null if there is no * such value. Even logic is similar to hasDuplicateValues, code had to be * duplicated because of different handling of null. * //from w w w . j ava2 s . c om * @param c * to search for duplicate value * @return first duplicate value */ public static <E> E firstDuplicateValue(Collection<E> c) { Iterator<E> iterator = c.iterator(); int idx = 0; while (iterator.hasNext()) { idx++; E e = iterator.next(); if (e != null && iterator.hasNext()) { Iterator<E> subiterator = c.iterator(); // move subiterator to index int sub = 0; while (subiterator.hasNext() && sub < idx) { sub++; subiterator.next(); } // check if others are duplicates while (subiterator.hasNext()) { E s = subiterator.next(); if (e.equals(s)) { return e; } } } } return null; }