List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
public static String collectionToSlashString(Collection<?> collection) { if (collection != null && collection.size() > 0) { StringBuilder sb = new StringBuilder(); Object obj = null;// www .j ava 2 s . c o m for (Iterator<?> iterator = collection.iterator(); iterator.hasNext();) { obj = iterator.next(); sb.append(obj.toString()); if (iterator.hasNext()) { sb.append("/"); } } return sb.toString(); } else { return ""; } }
From source file:Main.java
/** * Join a bunch of objects into a string delimited by the separator * * @param sep//from w w w . ja v a 2 s .c o m * @param c * @return */ public static String join(final String sep, final Collection<? extends Object> c) { if (c == null) { return null; } if (c.size() == 1) { return String.valueOf(c.iterator().next()); } if (c.size() == 0) { return ""; } final StringBuilder sb = new StringBuilder(); String s = ""; for (final Object o : c) { sb.append(s).append(String.valueOf(o)); s = sep; } return sb.toString(); }
From source file:Main.java
/** * Joins the elements of the provided <code>Collection</code> into * a single String containing the provided elements. * * No delimiter is added before or after the list. Null objects or empty * strings within the iteration are represented by empty strings. * * See the examples here: {@link #join(Object[],char)}. * * @param collection the <code>Collection</code> of values to join together, may be null * @param separator the separator character to use * @return the joined String, <code>null</code> if null iterator input * @since 2.3//from w w w. j a v a2 s .c o m */ public static String join(Collection collection, char separator) { if (collection == null) { return null; } return join(collection.iterator(), separator); }
From source file:Main.java
public static String[] asArray(Object object) { if (object == null) { return null; }//from w w w . j a v a 2 s.c o m Class componentType = object.getClass().getComponentType(); if (String.class.equals(componentType)) { return (String[]) object; } else if (componentType != null) { Object[] objects = (Object[]) object; String[] result = new String[objects.length]; for (int i = 0; i < objects.length; i++) { Object o = objects[i]; if (o == null) { continue; } result[i] = o.toString(); } return result; } else if (object instanceof Collection) { Collection collection = (Collection) object; String[] result = new String[collection.size()]; Iterator iterator = collection.iterator(); for (int i = 0; i < result.length; i++) { Object next = iterator.next(); if (next == null) { continue; } result[i] = next.toString(); } return result; } else { String string = object.toString().trim(); String[] split = string.split("\\s*,\\s*"); return split; } }
From source file:Main.java
public static <T> T getFirst(Collection<T> collection) { if (isEmpty(collection)) { return null; }/*from w w w. j a v a 2 s. com*/ if (collection instanceof List) { return ((List<T>) collection).get(0); } return collection.iterator().next(); }
From source file:Main.java
/** * <p>Joins the elements of the provided <code>Collection</code> into * a single String containing the provided elements.</p> * * <p>No delimiter is added before or after the list. * A <code>null</code> separator is the same as an empty String ("").</p> * * <p>See the examples here: {@link #join(Object[],String)}. </p> * * @param collection the <code>Collection</code> of values to join together, may be null * @param separator the separator character to use, null treated as "" * @return the joined String, <code>null</code> if null iterator input * @since 2.3/*from www. j a v a 2 s.c o m*/ */ public static String join(Collection collection, String separator) { if (collection == null) { return null; } return join(collection.iterator(), separator); }
From source file:Main.java
@SuppressWarnings("rawtypes") public static boolean hasUniqueObject(Collection<?> collection) { if (isEmpty(collection)) { return false; }/*from w ww .j a v a 2 s . c o m*/ boolean hasCandidate = false; Object candidate = null; for (Iterator it = collection.iterator(); it.hasNext();) { Object elem = it.next(); if (!hasCandidate) { hasCandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true; }
From source file:Main.java
/** * Find a single value of the given type in the given Collection. * @param collection the Collection to search * @param type the type to look for/*from w ww. j ava 2 s. c o m*/ * @return a value of the given type found if there is a clear match, * or <code>null</code> if none or more than one such value found */ public static Object findValueOfType(Collection collection, Class type) { if (isEmpty(collection)) { return null; } Object value = null; for (Iterator it = collection.iterator(); it.hasNext();) { Object obj = it.next(); if (type == null || type.isInstance(obj)) { if (value != null) { // More than one value found... no clear single value. return null; } value = obj; } } return value; }
From source file:Main.java
public static <T> List<Collection<T>> splitCollection(Collection<T> col, int numLists) throws InstantiationException, IllegalAccessException { List<Collection<T>> lstReturn = new ArrayList<Collection<T>>(); Iterator<T> iter = col.iterator(); for (int c = 0; c < numLists; c++) { Collection<T> temp = col.getClass().newInstance(); lstReturn.add(temp);/*ww w. ja va 2 s . co m*/ } while (iter.hasNext()) { for (int collectionIndex = 0; collectionIndex < numLists; collectionIndex++) { if (iter.hasNext()) { Collection<T> current = lstReturn.get(collectionIndex); T value = iter.next(); current.add(value); } } } return lstReturn; }
From source file:com.projity.util.DataUtils.java
/** * Apply a closure to one of the collections: if all is true, then use the allList, otherwise * use the nodeList, and extract only the impls of the type clazz (or subclasses of clazz) * @param closure/*from w ww . j a va 2 s . com*/ * @param all * @param allList * @param nodeList * @param clazz */ public static void forAllDo(Closure closure, boolean all, Collection allList, Collection nodeList, Class clazz) { forAllDo(closure, all, allList.iterator(), nodeList, clazz); }