List of usage examples for java.util Collection iterator
Iterator<E> iterator();
From source file:Main.java
/** * Creates a map with the elements of the collection as values using the * specified keyMethod to obtain the key from the elements. *///from ww w . j a va 2s. c o m @SuppressWarnings("unchecked") public static <K, T> Map<K, T> createMap(Collection<T> collection, String keyMethod) { Map<K, T> map = new HashMap<>(collection.size()); if (collection.isEmpty()) { return map; } Class<?> elementClass = collection.iterator().next().getClass(); Method getKeyMethod; try { getKeyMethod = elementClass.getMethod(keyMethod, new Class[0]); } catch (Exception e) { throw new RuntimeException("Failed to get key method", e); } for (T element : collection) { K key; try { key = (K) getKeyMethod.invoke(element, (Object[]) null); } catch (Exception e) { throw new RuntimeException("Failed to get key", e); } map.put(key, element); } return map; }
From source file:Main.java
/** * Returns <code>true</code> iff at least one element is in both collections15. * <p/>/*from w ww .ja v a 2 s .co m*/ * In other words, this method returns <code>true</code> iff the * {@link #intersection} of <i>coll1</i> and <i>coll2</i> is not empty. * * @param coll1 the first collection, must not be null * @param coll2 the first collection, must not be null * @return <code>true</code> iff the intersection of the collections15 is non-empty * @see #intersection * @since 2.1 */ public static <E> boolean containsAny(final Collection<? extends E> coll1, final Collection<? extends E> coll2) { if (coll1.size() < coll2.size()) { for (Iterator it = coll1.iterator(); it.hasNext();) { if (coll2.contains(it.next())) { return true; } } } else { for (Iterator it = coll2.iterator(); it.hasNext();) { if (coll1.contains(it.next())) { return true; } } } return false; }
From source file:Main.java
/** * Returns <code>true</code> iff all elements of {@code coll2} are also contained * in {@code coll1}. The cardinality of values in {@code coll2} is not taken into account, * which is the same behavior as {@link Collection#containsAll(Collection)}. * <p>/*from w w w .j a v a 2s .com*/ * In other words, this method returns <code>true</code> iff the * {@link #intersection} of <i>coll1</i> and <i>coll2</i> has the same cardinality as * the set of unique values from {@code coll2}. In case {@code coll2} is empty, {@code true} * will be returned. * <p> * This method is intended as a replacement for {@link Collection#containsAll(Collection)} * with a guaranteed runtime complexity of {@code O(n + m)}. Depending on the type of * {@link Collection} provided, this method will be much faster than calling * {@link Collection#containsAll(Collection)} instead, though this will come at the * cost of an additional space complexity O(n). * * @param coll1 the first collection, must not be null * @param coll2 the second collection, must not be null * @return <code>true</code> iff the intersection of the collections has the same cardinality * as the set of unique elements from the second collection * @since 4.0 */ public static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2) { if (coll2.isEmpty()) { return true; } else { final Iterator<?> it = coll1.iterator(); final Set<Object> elementsAlreadySeen = new HashSet<Object>(); for (final Object nextElement : coll2) { if (elementsAlreadySeen.contains(nextElement)) { continue; } boolean foundCurrentElement = false; while (it.hasNext()) { final Object p = it.next(); elementsAlreadySeen.add(p); if (nextElement == null ? p == null : nextElement.equals(p)) { foundCurrentElement = true; break; } } if (foundCurrentElement) { continue; } else { return false; } } return true; } }
From source file:com.projity.grouping.core.model.NodeModelUtil.java
private static void extractNodeList(NodeModel nodeModel, Node parent, Collection result) { if (parent != null) result.add(parent);// ww w .ja v a2 s . c o m Collection children = nodeModel.getChildren(parent); if (children != null) { Iterator i = children.iterator(); while (i.hasNext()) { Node n = (Node) i.next(); extractNodeList(nodeModel, n, result); } } }
From source file:Main.java
/** * /* w w w .ja v a 2s .co m*/ * @param collection * @return float[] */ public static float[] toFloatArray(final Collection<Float> collection) { final float[] array = new float[collection == null ? 0 : collection.size()]; if (collection != null) { int i = 0; for (Iterator<Float> iterator = collection.iterator(); iterator.hasNext();) { array[i++] = iterator.next().floatValue(); } } return array; }
From source file:Main.java
public static String join(Collection<?> input, String sep) { if (input == null || input.size() == 0) { return ""; }/*from www . j av a 2s. co m*/ StringBuilder sb = new StringBuilder(); int index = 0; int size = input.size(); Iterator<?> it = input.iterator(); while (it.hasNext()) { if (index == size - 1) { break; } Object o = it.next(); sb.append(o).append(sep); index++; } sb.append(it.next()); return sb.toString(); }
From source file:com.projity.grouping.core.model.NodeModelUtil.java
private static void dumpTask(NodeModel nodeModel, Node parent, String indent) { if (parent != null) System.out.println(indent + ">" + parent.toString()); Collection children = nodeModel.getChildren(parent); if (children != null) { Iterator i = children.iterator(); while (i.hasNext()) { Node n = (Node) i.next(); Object impl = n.getImpl(); if (impl instanceof Task) { if (((Task) impl).getWbsParentTask() != (parent == null ? null : parent.getImpl())) System.out.println("cached hierarchy error - child " + impl + " cached parent" + ((Task) impl).getWbsParentTask() + " parent " + parent.getImpl()); }/*from w w w . jav a2 s. co m*/ dumpTask(nodeModel, n, indent + "--"); } } }
From source file:edu.uci.ics.jung.utils.PredicateUtils.java
/** * Returns <code>true</code> if all elements of <code>c</code> * satisfy <code>p</code>./*from w w w . j a va2s . c o m*/ */ public static boolean satisfiesPredicate(Collection c, Predicate p) { for (Iterator iter = c.iterator(); iter.hasNext();) { if (!p.evaluate(iter.next())) return false; } return true; }
From source file:Main.java
/** * Constructs a single String by iterating over the elements in the * collection parameter, calling toString() on each method. * // w w w.j a va 2s. c o m * @param <T> * @param collection * @param separator * @return A separator-separated value String. */ public static <T extends Object> String toXsvString(Collection<T> collection, String separator) { if (collection == null) { return null; } StringBuffer sb = new StringBuffer(); int count = 0; for (Iterator<T> iter = collection.iterator(); iter.hasNext(); count++) { if (count > 0) { sb.append(separator); } sb.append(iter.next().toString()); } return sb.toString(); }
From source file:Main.java
public static <T> T checkAndReturnOnlyElement(Collection<T> collection) { if (collection == null || collection.size() == 0) { return null; } else if (collection.size() != 1) { throw new IllegalStateException("collection size is " + collection.size()); }//from w w w .ja va 2 s . c om return collection.iterator().next(); }