List of usage examples for java.util Iterator next
E next();
From source file:Main.java
/** * @param coll {@link Collection} any collection object containing zero or more elements,can't be null. * @return {@link Number} the maximum value available in the collection. *//*from w w w . j a v a 2 s . co m*/ public static Number max(Collection<? extends Number> coll) { Iterator<? extends Number> i = coll.iterator(); Number candidate = i.next(); while (i.hasNext()) { Number next = i.next(); if (next.doubleValue() - candidate.doubleValue() > 0) candidate = next; } return candidate; }
From source file:Main.java
/** * @throws IllegalArgumentException if an add operation "soft fails" (does not modify the collection) * @see Collection#addAll(Collection)//from www. j a va2 s. co m */ public static <E> void addAllForce(Collection<E> collection, Iterator<? extends E> add) { while (add.hasNext()) { E next = add.next(); Preconditions.checkArgument(collection.add(next), "collection did not accept next element %s: %s", next, collection); } }
From source file:Main.java
/** * Returns true if both collections contain the same elements and the elements are in the same order. * Note that the start elements do not have to be the equal! * @param s1 The first collection/* w ww.j a v a2 s.c o m*/ * @param s2 The second collection * @return true if s1 and s2 are same in the sense explained above. */ public static boolean sameElementsSameOrder(Collection<?> s1, Collection<?> s2) { if (s1.size() != s2.size()) return false; Iterator<?> it1 = s1.iterator(); Object first = it1.next(); for (Iterator<?> it2 = s2.iterator(); it2.hasNext();) { if (it2.next().equals(first)) { while (it1.hasNext()) { if (!it2.hasNext()) it2 = s2.iterator(); if (!it1.next().equals(it2.next())) return false; } return true; } } return false; }
From source file:Main.java
public static <E> E getElementAtIndex(Collection<E> set, int idx) { if (idx >= set.size() || idx < 0) { throw new IndexOutOfBoundsException(); }//from w w w . jav a 2 s . c om Iterator<E> it = set.iterator(); for (int i = 0; i < idx; ++i) { it.next(); } return it.next(); }
From source file:Main.java
public static <T> T getFirst(Iterable<? extends T> iterable, T def) { final Iterator<? extends T> iter = iterable.iterator(); return iter.hasNext() ? iter.next() : def; }
From source file:com.apextom.util.CollectionUtil.java
/** * ? null .//from w w w. j av a2s. c o m * * @param list */ public static void removeNull(Collection<Object> list) { if (list == null || list.size() == 0) { return; } Iterator<Object> iter = list.iterator(); while (iter.hasNext()) { if (iter.next() == null) { iter.remove(); } } }
From source file:Main.java
public static List<String> removeIgnoreCase(List<String> l, String s) { Iterator<String> iter = l.iterator(); while (iter.hasNext()) { if (iter.next().equalsIgnoreCase(s)) { iter.remove();/*ww w .java 2 s . co m*/ break; } } return l; }
From source file:Main.java
public static <T> T first(Collection<T> collection) { Iterator<T> iterator = collection.iterator(); return iterator.hasNext() ? iterator.next() : null; }
From source file:Main.java
public static <T> T getAt(Collection<T> col, int index) { if (col instanceof List) return ((List<T>) col).get(index); int i;/*from w w w . j a v a2 s .co m*/ Iterator<T> it; for (i = 0, it = col.iterator(); i < index && it.hasNext(); it.next()) ; return it.hasNext() ? it.next() : null; }
From source file:Main.java
public static String getClasspathString(List<File> c) { StringBuilder sb = new StringBuilder(); Iterator<File> i = c.iterator(); if (i.hasNext()) { sb.append(i.next().getAbsolutePath()); while (i.hasNext()) { sb.append(File.pathSeparatorChar); sb.append(i.next().getAbsolutePath()); }//from w w w . j a v a2 s. co m } return sb.toString(); }