List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
/** * Joins the elements in an input collection using a given separator. * * @param <A> Key type// www . ja va2 s. c o m * @param collection Input collection * @param separator Separator * @return {@code String} representation of the input {@code collection} */ public static <A> String join(Collection<A> collection, String separator) { if (collection.isEmpty()) return ""; Iterator<A> it = collection.iterator(); StringBuilder out = new StringBuilder(); out.append(it.next()); while (it.hasNext()) out.append(separator).append(it.next()); return out.toString(); }
From source file:Main.java
public static Object oneOrMany(final Collection<?> collection) { switch (size(collection)) { case 0:/*www. j a v a 2 s . c om*/ return null; case 1: return collection.iterator().next(); default: return 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//from ww w .ja 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
/** * Returns the given Collection as formatted String *//*w w w .j a va 2 s . com*/ public static String toString(Collection<?> c) { StringBuilder buffer = new StringBuilder(); Iterator it = c.iterator(); for (int i = 0; it.hasNext(); i++) { buffer.append(i).append(": ").append(it.next()).append('\n'); } // Delete the last \n if (buffer.length() > 1) { buffer.setLength(buffer.length() - 1); } return buffer.toString(); }
From source file:Main.java
public static boolean hasValue(Map<?, ?> map, String key, String val) { if (map == null) { return false; }/* w ww.j a v a2 s.c o m*/ Collection<?> col = (Collection<?>) map.get(key); if (col == null) { return false; } Iterator<?> itr = col.iterator(); while (itr.hasNext()) { String str = (String) itr.next(); if (str.equalsIgnoreCase(val)) { return true; } if (str.toLowerCase().startsWith(val)) { 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(); }/*w w w.ja v a 2 s. co m*/ Iterator<E> it = set.iterator(); for (int i = 0; i < idx; ++i) { it.next(); } return it.next(); }
From source file:Main.java
@SuppressWarnings("rawtypes") public static boolean allNotIn(Collection source, Collection target) { if (target == null || target.size() == 0) { return true; }// ww w . j a va 2 s. co m for (Iterator it = source.iterator(); it.hasNext();) { Object candidate = it.next(); if (target.contains(candidate)) { return false; } } return true; }
From source file:Main.java
/** * Return the last element of a collection. This method iterates over the complete collection to found * the last element.//from w w w . j ava 2 s . c o m * * @param collection The collection to get the last element from. * @param <T> The type of value. * @return The last element of the collection. If there is no element, the method return null. */ public static <T> T last(Collection<T> collection) { if (collection.isEmpty()) { return null; } Iterator<T> iterator = collection.iterator(); for (int i = 0; i < collection.size() - 1; i++) { iterator.next(); } return iterator.next(); }
From source file:Main.java
/** * Returns a {@link Map} mapping each unique element in the given * {@link Collection} to an {@link Integer} representing the number * of occurrences of that element in the {@link Collection}. * <p>/* w w w . j a v a 2 s . c o m*/ * Only those elements present in the collection will appear as * keys in the map. * * @param coll the collection to get the cardinality map for, must not be null * @return the populated cardinality map */ public static Map getCardinalityMap(final Collection coll) { Map count = new HashMap(); for (Iterator it = coll.iterator(); it.hasNext();) { Object obj = it.next(); Integer c = (Integer) (count.get(obj)); if (c == null) { count.put(obj, INTEGER_ONE); } else { count.put(obj, new Integer(c.intValue() + 1)); } } return count; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T[] collectionToArray(Collection<T> sourceCollection, Class<?> elementClass) { Object array = Array.newInstance(elementClass, sourceCollection.size()); Iterator iterator = sourceCollection.iterator(); for (int i = 0; i < sourceCollection.size(); i++) { Array.set(array, i, iterator.next()); }//from w w w . j av a 2 s . c om return (T[]) array; }