Example usage for java.util Collection iterator

List of usage examples for java.util Collection iterator

Introduction

In this page you can find the example usage for java.util Collection iterator.

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this collection.

Usage

From source file:Main.java

public static String join(final Collection<?> collection, final String separator) {
    if (collection == null || collection.size() == 0) {
        return null;
    }//from w w w  .  j ava  2  s  .c  o  m
    StringBuilder sb = new StringBuilder(collection.size() * 7);
    Iterator<?> iterator = collection.iterator();
    sb.append(iterator.next());
    while (iterator.hasNext()) {
        sb.append(separator);
        sb.append(iterator.next());
    }
    return sb.toString();
}

From source file:gov.nih.nci.ncicb.cadsr.common.util.SessionUtils.java

private static void clearStaleObject() {
    synchronized (sessionObjectCacheTimeout) {
        log.error("SessionUtil.clearStaleObject start :" + TimeUtils.getEasternTime());
        Set keys = sessionObjectCacheTimeout.keySet();
        Collection copyKeys = new ArrayList();
        keys.addAll(copyKeys); // done to avoid java.util.ConcurrentModificationException
        if (keys != null) {
            Iterator it = copyKeys.iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                Long storedTime = (Long) sessionObjectCacheTimeout.get(key);
                if (new Long(new Date().getTime()).longValue() > storedTime.longValue() + CACHE_TIMEOUT_VALUE) {
                    sessionObjectCache.remove(key);
                    sessionObjectCacheTimeout.remove(key);
                }/*w  ww. j a va 2s  . co  m*/
            }
        }
        log.error("SessionUtil.clearStaleObject( start :" + TimeUtils.getEasternTime());
    }
}

From source file:Main.java

public static boolean equalsList(Collection<?> c1, Collection<?> c2) {
    boolean equals;
    if (c1 == null) {
        equals = (c2 == null);/*from  w  ww .  j  ava 2s  . com*/
    } else if (c2 == null) {
        equals = false;
    } else if (c1.size() == c2.size()) {
        equals = true;
        Iterator<?> iterC1 = c1.iterator();
        Iterator<?> iterC2 = c2.iterator();
        while (equals && iterC1.hasNext() && iterC2.hasNext()) {
            Object o1 = iterC1.next();
            Object o2 = iterC2.next();
            equals = o1.equals(o2);
        }
    } else {
        equals = false;
    }
    return equals;
}

From source file:com.hmsinc.epicenter.surveillance.notification.EventNotifierUtils.java

private static String makeAttributeString(final Collection<? extends Attribute> attributes) {
    final StringBuilder sb = new StringBuilder();
    if (attributes.size() == 1) {
        sb.append(attributes.iterator().next().getName().toLowerCase());
    } else if (attributes.size() > 1) {
        int i = 0;
        for (Attribute a : attributes) {
            i++;/*from   w w  w  . ja  v  a2s.c o m*/
            if (sb.length() > 0) {
                if (i == attributes.size()) {
                    sb.append(" or ");
                } else {
                    sb.append(", ");
                }
            }
            sb.append(a.getName().toLowerCase());
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Append a collection of strings and delimiter.
 *
 * @param c A collection of strings./*from  ww w.j av a  2  s. co  m*/
 * @param delimiter A delimiter string.
 * @return The new string.
 */
public static String join(Collection<String> c, String delimiter) {
    if (c == null || delimiter == null) {
        throw new IllegalArgumentException("Collections and delimiters given to join cannot be null!");
    }
    StringBuilder builder = new StringBuilder("");
    Iterator<String> iter = c.iterator();
    while (iter.hasNext()) {
        builder.append(iter.next());
        if (iter.hasNext()) {
            builder.append(delimiter);
        }
    }
    return builder.toString();
}

From source file:CollectionUtils.java

/**
 * Return <code>true</code> if any element in '<code>candidates</code>' is
 * contained in '<code>source</code>'; otherwise returns <code>false</code>.
 * @param source the source Collection/*from w  w  w.ja v  a 2s.  c om*/
 * @param candidates the candidates to search for
 * @return whether any of the candidates has been found
 */
public static boolean containsAny(Collection source, Collection candidates) {
    if (isEmpty(source) || isEmpty(candidates)) {
        return false;
    }
    for (Iterator it = candidates.iterator(); it.hasNext();) {
        if (source.contains(it.next())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static <T1, T2> Collection<T2> lazyMap(final Collection<T1> lst, final Function<T1, T2> mapper) {
    return new AbstractCollection<T2>() {
        @Override/*www  .  ja  v  a 2 s. c o m*/
        public Iterator<T2> iterator() {
            return new Iterator<T2>() {
                Iterator<T1> impl = lst.iterator();

                @Override
                public boolean hasNext() {
                    return impl.hasNext();
                }

                @Override
                public T2 next() {
                    return mapper.apply(impl.next());
                }

                @Override
                public void remove() {
                    impl.remove();
                }
            };
        }

        @Override
        public int size() {
            return lst.size();
        }
    };
}

From source file:edu.uci.ics.jung.utils.PredicateUtils.java

public static Collection getSatisfyingElements(Collection c, Predicate p) {
    Collection satisfied = new LinkedList();
    for (Iterator iter = c.iterator(); iter.hasNext();) {
        Object o = iter.next();//  w w w . j  ava  2  s . co  m
        if (p.evaluate(o))
            satisfied.add(o);
    }
    return satisfied;
}

From source file:Main.java

public static <T> boolean doCompare(Collection<T> c1, Collection<T> c2) {

    if (c1 == null && c2 == null) {
        return true;
    }//from  w w  w  .ja v  a2  s. c o m

    if (c1 == null || c2 == null) {
        return false;
    }

    if (c1.size() != c2.size()) {
        return false;
    }

    Iterator<T> it = c1.iterator();
    while (it.hasNext()) {
        if (!c2.contains(it.next())) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

public static <K, V> Map<K, V> collectionToMap(Collection<K> keyCollection, Collection<V> valueCollection,
        Map<K, V> targetMap) {
    if (isEmpty(keyCollection)) {
        return targetMap;
    }/*from  www  .  ja va 2s . co  m*/
    if (valueCollection == null)
        valueCollection = new ArrayList<>();

    Iterator<K> keyIterator = keyCollection.iterator();
    Iterator<V> valueIterator = valueCollection.iterator();
    while (keyIterator.hasNext()) {
        V value = valueIterator.hasNext() ? valueIterator.next() : null;
        targetMap.put(keyIterator.next(), value);
    }
    return targetMap;
}