List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
public static Collection and(Collection collection1, Collection collection2) { Vector completeList = new Vector(); if (collection2 == null) return completeList; Iterator i = collection2.iterator(); while (i.hasNext()) { Object addlItem = i.next(); if (collection1.contains(addlItem) == true) { completeList.addElement(addlItem); }//from w w w. j a v a2 s .c o m } return completeList; }
From source file:Main.java
/** * Get the first element of the passed collection. * * @param <ELEMENTTYPE>// w w w.j a v a 2 s . c om * Collection element type * @param aCollection * The collection. May be <code>null</code>. * @return <code>null</code> if the collection is <code>null</code> or empty, * the first element otherwise. */ @Nullable public static <ELEMENTTYPE> ELEMENTTYPE getFirstElement(@Nullable final Collection<ELEMENTTYPE> aCollection) { return isEmpty(aCollection) ? null : aCollection.iterator().next(); }
From source file:Main.java
@SuppressWarnings("rawtypes") public static boolean allIn(Collection source, Collection target) { if (target == null || target.size() == 0) { return false; }/* w w w .ja v a 2 s . co m*/ for (Iterator it = source.iterator(); it.hasNext();) { Object object = it.next(); if (!target.contains(object)) { return false; } } return true; }
From source file:Main.java
/** * Find a value of the given type in the given collection. * @param coll the collection to search/*from w w w .j a va 2 s . c om*/ * @param type the type to look for * @return a value of the given type found, or <code>null</code> if none * @throws IllegalArgumentException if more than one value * of the given type found */ public static Object findValueOfType(Collection<?> coll, Class<?> type) throws IllegalArgumentException { Object value = null; for (Iterator<?> it = coll.iterator(); it.hasNext();) { Object obj = it.next(); if (type.isInstance(obj)) { if (value != null) { throw new IllegalArgumentException( "More than one value of type [" + type.getName() + "] found"); } value = obj; } } return value; }
From source file:Main.java
public static String collectionToString(Collection c) { StringBuffer buff = new StringBuffer(); int i = 0;/* w w w .j a va2 s . c o m*/ int size = c.size(); for (Iterator iter = c.iterator(); iter.hasNext();) { Object object = iter.next(); if (size > 1 && i == (size - 1)) { buff.append("and "); } buff.append(object); if (i < (size - 1)) { if (size > 2) { buff.append(", "); } else { buff.append(" "); } } i++; } return buff.toString(); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <S, D extends S> D[] downcastToArray(Collection<S> source, Class<D> destClass) { D[] dest = (D[]) Array.newInstance(destClass, source.size()); Iterator<S> it = source.iterator(); for (int i = 0; i < dest.length; i++) { S s = it.next();//from w w w.j a va 2 s . com if (destClass.isAssignableFrom(s.getClass())) dest[i] = (D) s; else throw new IllegalArgumentException("Could not downcast element from class " + s.getClass().getSimpleName() + " to " + destClass.getSimpleName()); } return dest; }
From source file:Main.java
public static Collection<Object> filterCollection(Collection<Object> src, Collection<Object> dest) { if (isEmpty(dest)) return src; Iterator<Object> iterator = src.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); if (dest.contains(next)) { iterator.remove();/*from w w w . j a va2s .c o m*/ } } return src; }
From source file:com.jdy.ddj.common.utils.Collections3.java
public static <T> List<T> subtract(final Collection<T> a, final Collection<T> b) { ArrayList<T> list = new ArrayList<T>(a); for (Iterator it = b.iterator(); it.hasNext();) { list.remove(it.next());/*from ww w .ja v a 2 s . c o m*/ } return list; }
From source file:Main.java
/** * Returns the first element of the given collection * /*from w w w . ja v a 2s . c om*/ * @param <T> * @param collection * @return <code>null</code> if the first element is <code>null</code> or * the collection is <code>null</code> or empty */ public static <T> T firstElementOf(final Collection<? extends T> collection) { if (isEmpty(collection)) { return null; } return collection.iterator().next(); }
From source file:Main.java
public static <T> T only(Collection<T> coll) { if (coll.size() != 1) { throw new NoSuchElementException("Collection has none or more than one elements"); }//from w w w. j a v a 2 s . co m return coll.iterator().next(); }