List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
public static String toString(Collection<?> list, String split) { if (list == null) { return null; }/*from ww w . j av a2s . c o m*/ StringBuffer rs = new StringBuffer(); Iterator<?> it = list.iterator(); while (it.hasNext()) { rs.append(it.next()); if (it.hasNext()) { rs.append(split); } } return rs.toString(); }
From source file:Main.java
/** * Converts the given collection to a <code>java.util.List</code>. * @param collection The collection to convert to a list. * @return A <code>java.util.List</code> containing all of the elements from the * supplied collection.//from ww w . ja v a 2 s . com */ public static List toList(Collection collection) { if (collection instanceof List) return (List) collection; List list = new ArrayList(); Iterator iterator = collection.iterator(); while (iterator.hasNext()) { list.add(iterator.next()); } return list; }
From source file:Main.java
public static <T> T last(Collection<T> collection) { if (collection == null || collection.isEmpty()) { return null; }/*from w ww.ja v a 2 s . c o m*/ Iterator<T> iterator = collection.iterator(); T elem = null; while (iterator.hasNext()) { elem = iterator.next(); } return elem; }
From source file:Main.java
/** * Computes the array of element indices which where added to a collection. * * @param smallCollection//from w w w. ja v a 2 s . c o m * the original collection. * @param bigCollection * the collection with added elements. * @return the the array of element indices which where added to the original * collection */ public static int[] computeDifferenceIndices(Collection<?> smallCollection, Collection<?> bigCollection) { List<Integer> addedIndices = new ArrayList<>(); int index = 0; for (Iterator<?> ite = bigCollection.iterator(); ite.hasNext(); index++) { if (smallCollection == null || !smallCollection.contains(ite.next())) { if (smallCollection == null) { ite.next(); } addedIndices.add(index); } } int[] result = new int[addedIndices.size()]; for (int i = 0; i < addedIndices.size(); i++) { result[i] = addedIndices.get(i); } return result; }
From source file:Main.java
/** * Returns the element at the specified position in the * collection. // w ww . j av a2s .co m * Shifts any subsequent elements to the left * (subtracts one from their indices). * Returns the element that was removed from the Collection. * @param p_collection a Collection * @param p_index index of element to return. * @returns the element at the specified position in the * collection. */ public static Object get(Collection p_collection, int p_index) { if (p_collection == null) { return null; } Iterator it = p_collection.iterator(); for (int i = 0; it.hasNext(); i++) { if (i == p_index) { return it.next(); } } return null; }
From source file:Main.java
public static <A> Set<A> makeSet(Collection<A> xs) { if (xs.size() == 0) return Collections.<A>emptySet(); else if (xs.size() == 1) return Collections.singleton(xs.iterator().next()); else {/*from w w w . ja v a 2 s . c o m*/ Set<A> set = new HashSet<A>(xs.size()); set.addAll(xs); return set; } }
From source file:org.apache.bigtop.bigpetstore.qstream.Utils.java
/** * Borrowed from apache StringUtils./*from w w w . j av a 2 s . com*/ */ public static String join(Collection var0, Object var1) { StringBuffer var2 = new StringBuffer(); for (Iterator var3 = var0.iterator(); var3.hasNext(); var2.append(var3.next())) { if (var2.length() != 0) { var2.append(var1); } } return var2.toString(); }
From source file:Main.java
/** * Determine whether the given collection only contains a * single unique object./* w ww.j a v a 2s . c om*/ * @param coll the collection to check * @return <code>true</code> if the collection contains a * single reference or multiple references to the same * instance, <code>false</code> else */ public static boolean hasUniqueObject(Collection<?> coll) { if (coll.isEmpty()) { return false; } Object candidate = null; for (Iterator<?> it = coll.iterator(); it.hasNext();) { Object elem = it.next(); if (candidate == null) { candidate = elem; } else if (candidate != elem) { return false; } } return true; }
From source file:com.enonic.cms.core.structure.menuitem.MenuItemKey.java
public static String convertToCommaSeparatedString(Collection<MenuItemKey> keys) { StringBuffer s = new StringBuffer(); for (Iterator<MenuItemKey> it = keys.iterator(); it.hasNext();) { MenuItemKey menuItemKey = it.next(); s.append(menuItemKey);// ww w . j ava 2s. c om if (it.hasNext()) { s.append(","); } } return s.toString(); }
From source file:Main.java
public static <T extends Object> T remove(final int index, final Collection<T> collection) { if (!isEmptyOrNull(collection)) { int i = 0; for (Iterator<T> it = collection.iterator(); it.hasNext();) { final T removed = it.next(); if (i == index) { it.remove();//from ww w. j av a 2 s . co m return removed; } i++; } } return null; }