List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
/** * Get comma separated string from collection * /* w ww.j a v a 2 s .c o m*/ * @param strings * @return */ public static String getCommaSeparatedString(Collection<String> strings) { if (strings != null && !strings.isEmpty()) { Iterator<String> it = strings.iterator(); StringBuilder sb = new StringBuilder(it.next()); while (it.hasNext()) { sb.append("," + it.next()); } return sb.toString(); } else { return null; } }
From source file:Main.java
public static String join(Collection<?> coll, String delimiter) { if (coll.isEmpty()) { return ""; }// w w w .ja va 2 s . com Iterator<?> it = coll.iterator(); String s = String.valueOf(it.next()); while (it.hasNext()) { s += delimiter; s += String.valueOf(it.next()); } return s; }
From source file:Main.java
/** * Creates new collection that contains only element whose string * representation starts with prefix It is parameterized. * // w w w. j a v a2 s. com * @param c * raw collection to create prefixed collection from * @param prefix * to use as filter * @return collection without nulls */ public static <T> Collection<T> withPrefix(Collection<T> c, String prefix) { Collection<T> prefixed = new ArrayList<T>(); Iterator<T> iterator = c.iterator(); while (iterator.hasNext()) { T o = iterator.next(); if (o != null && o.toString().startsWith(prefix)) { prefixed.add(o); } } return prefixed; }
From source file:Main.java
public static Object findFirstMatch(Collection source, Collection candidates) { if (!isEmpty(source) && !isEmpty(candidates)) { Iterator it = candidates.iterator(); Object candidate;/*from w ww .ja v a 2 s .c o m*/ do { if (!it.hasNext()) { return null; } candidate = it.next(); } while (!source.contains(candidate)); return candidate; } else { return null; } }
From source file:Main.java
public static <K, V> Map<K, V> collectionToMap(Collection<V> collection, String key) { Map<K, V> map = null;/*from w ww . j a v a 2s .c o m*/ Iterator<V> iterator = collection.iterator(); try { if (key == null) throw new Exception("no key filed"); else { map = new HashMap<K, V>(); while (iterator.hasNext()) { V v = iterator.next(); Class<? extends Object> c = v.getClass(); Field field = c.getField(key); field.setAccessible(true); map.put((K) field.get(v), v); } } } catch (Exception e) { e.printStackTrace(); } return map; }
From source file:Main.java
public static Collection intersection(Collection a, Collection b) { Collection results = new HashSet(); for (Iterator i = a.iterator(); i.hasNext();) { Object o = i.next();//from ww w. jav a 2 s. co m if (b.contains(o)) { results.add(o); } } return results; }
From source file:Main.java
/** * Return the first element of the collection. * * @param collection The collection.//from w w w . j a v a2s. co m * @param <T> The objects stored in the collection. * * @return The first element of the collection or null if the collection is empty. */ public static <T> T first(Collection<T> collection) { if (collection.isEmpty()) { return null; } return collection.iterator().next(); }
From source file:com.apextom.util.CollectionUtil.java
/** * ? null .//from ww w. java 2 s . co 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
/** * Check whether the given Collection contains the given element instance. * <p>Enforces the given instance to be present, rather than returning * <code>true</code> for an equal element as well. * @param collection the Collection to check * @param element the element to look for * @return <code>true</code> if found, <code>false</code> else *//*from w ww . j a va2 s. co m*/ public static boolean containsInstance(Collection<?> collection, Object element) { if (collection != null) { for (Iterator<?> it = collection.iterator(); it.hasNext();) { Object candidate = it.next(); if (candidate == element) { return true; } } } return false; }
From source file:Main.java
public static boolean isEmptyCollection(Collection<?> cols) { if (cols == null || cols.isEmpty()) { return true; }//from w w w. ja va 2 s . co m Iterator<?> ite = cols.iterator(); while (ite.hasNext()) { if (ite.next() == null) { return true; } } return false; }