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

/**
 * Returns a {@link Map} mapping each unique element in the given
 * {@link Collection} to an {@link Integer} representing the number of
 * occurances of that element in the {@link Collection}. An entry that maps
 * to <tt>null</tt> indicates that the element does not appear in the given
 * {@link Collection}./*  w  w w  . j a v  a 2  s  .  co  m*/
 */
public static Map getCardinalityMap(final Collection col) {
    HashMap count = new HashMap();
    Iterator it = col.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        Integer c = (Integer) (count.get(obj));
        if (null == c) {
            count.put(obj, new Integer(1));
        } else {
            count.put(obj, new Integer(c.intValue() + 1));
        }
    }
    return count;
}

From source file:Main.java

/**
 * Returns the first item in the collection or null if the collection is empty
 *//*from w  ww.  j a v  a2s .  co  m*/
public static <X> X getFirstItem(Collection<X> c) {
    X o;
    if (c == null || c.isEmpty()) {
        o = null;
    } else if (c instanceof List) {
        o = ((List<X>) c).get(0);
    } else {
        o = c.iterator().next();
    }
    return o;
}

From source file:com.vmware.identity.rest.core.client.URIFactory.java

@SuppressWarnings("unchecked")
public static URI buildURI(HostRetriever host, String path, Object... args) throws ClientException {
    Map<String, Object> parameters = null;

    if (args != null) {
        if (args[args.length - 1] instanceof Map) {
            parameters = (Map<String, Object>) args[args.length - 1];
            args = Arrays.copyOf(args, args.length - 1);
        }// w  ww  . j  a v  a  2 s. c om
    }

    URIBuilder builder = host.getURIBuilder().setPath(String.format(path, args));

    if (parameters != null && !parameters.isEmpty()) {
        for (Entry<String, Object> param : parameters.entrySet()) {
            if (param.getValue() instanceof Collection) {
                Collection<Object> list = (Collection<Object>) param.getValue();
                Iterator<Object> iter = list.iterator();

                while (iter.hasNext()) {
                    builder.setParameter(param.getKey(), iter.next().toString());
                }

            } else {
                builder.setParameter(param.getKey(), param.getValue().toString());
            }
        }
    }

    try {
        return builder.build();
    } catch (URISyntaxException e) {
        throw new ClientException("An error occurred while building the URI", e);
    }
}

From source file:com.gistlabs.mechanize.document.json.node.impl.AbstractJsonNode.java

static String join(final Collection<?> s, final String delimiter) {
    StringBuilder builder = new StringBuilder();
    Iterator<?> iter = s.iterator();
    while (iter.hasNext()) {
        builder.append(iter.next());/*  w  w  w.j  a va 2 s  .com*/
        if (!iter.hasNext())
            break;
        builder.append(delimiter);
    }
    return builder.toString();
}

From source file:Main.java

public static <T extends Comparable<? super T>> int compareAsList(@Nonnull Collection<? extends T> list1,
        @Nonnull Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;
    Iterator<? extends T> elements2 = list2.iterator();
    for (T element1 : list1) {
        res = element1.compareTo(elements2.next());
        if (res != 0)
            return res;
    }//from   w w  w.  jav a  2 s .c  om
    return 0;
}

From source file:Main.java

public static <T> int compareAsList(@Nonnull Comparator<? super T> elementComparator,
        @Nonnull Collection<? extends T> list1, @Nonnull Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;
    Iterator<? extends T> elements2 = list2.iterator();
    for (T element1 : list1) {
        res = elementComparator.compare(element1, elements2.next());
        if (res != 0)
            return res;
    }/*from  w w w.j  a v  a  2  s  .  c o m*/
    return 0;
}

From source file:Main.java

/**
 * Removes duplicate objects from the Collection.
 * @param p_collection a Collection/*from  ww  w .ja va2  s .c  o  m*/
 * @returns true if one or more duplicate objects were removed.
 */
public static boolean removeDuplicates(Collection p_collection) {
    if (p_collection == null) {
        return false;
    }
    HashSet set = new HashSet(p_collection.size());
    Iterator it = p_collection.iterator();
    while (it.hasNext()) {
        set.add(it.next());
    }
    if (set.size() != p_collection.size()) {
        p_collection.clear();
        p_collection.addAll(set);
        return true;
    }
    return false;
}

From source file:Main.java

public static <T extends Comparable<? super T>> int compareAsList(Collection<? extends T> list1,
        Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;
    Iterator<? extends T> elements2 = list2.iterator();
    for (T element1 : list1) {
        res = element1.compareTo(elements2.next());
        if (res != 0)
            return res;
    }//w  w w. j a  v a 2s.  com
    return 0;
}

From source file:Main.java

public static <T> int compareAsList(Comparator<? super T> elementComparator, Collection<? extends T> list1,
        Collection<? extends T> list2) {
    int res = Ints.compare(list1.size(), list2.size());
    if (res != 0)
        return res;
    Iterator<? extends T> elements2 = list2.iterator();
    for (T element1 : list1) {
        res = elementComparator.compare(element1, elements2.next());
        if (res != 0)
            return res;
    }/*w w w. j av  a2 s .c  o  m*/
    return 0;
}

From source file:Main.java

public static Object findValueOfType(Collection collection, Class type) {
    if (isEmpty(collection)) {
        return null;
    } else {//from  w w  w .  j av  a  2  s  .co m
        Object value = null;
        Iterator it = collection.iterator();

        while (true) {
            Object obj;
            do {
                if (!it.hasNext()) {
                    return value;
                }

                obj = it.next();
            } while (type != null && !type.isInstance(obj));

            if (value != null) {
                return null;
            }

            value = obj;
        }
    }
}