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 int cardinality(java.lang.Object obj, Collection coll) {
    int numObject = 0;
    if (coll != null) {
        Iterator iter = coll.iterator();

        while (iter.hasNext()) {
            Object collObj = iter.next();

            if (collObj != null) {
                if (collObj.equals(obj)) {
                    numObject++;// w w  w. j  a v  a2 s  .  co m
                }
            }

        }
    }
    return numObject;
}

From source file:Main.java

public static <T> T firstElementOf(final Collection<? extends T> items) {
    if (items != null && !items.isEmpty()) {
        return items.iterator().next();
    }// w  w w . jav a2  s  .c o  m
    return null;
}

From source file:Main.java

/**
 * @return een int array van alle elementen uit de <tt>collection</tt>.
 */// w  ww . java  2s . c  o  m
public static int[] toIntArray(Collection<Integer> collection) {
    int[] res = new int[collection.size()];
    Iterator<Integer> iterator = collection.iterator();
    for (int i = 0; i < collection.size(); i++) {
        res[i] = iterator.next();
    }
    return res;
}

From source file:Main.java

public static String toIdListString(Collection<Long> ids) {
    StringBuilder response = new StringBuilder();
    Iterator<Long> i = ids.iterator();
    while (i.hasNext()) {
        response.append(i.next());//from  ww  w .  j  a  v  a2 s . c  om
        if (i.hasNext())
            response.append(",");
    }
    return response.toString();
}

From source file:Main.java

public static float[] asFloatArray(Collection<Float> collection) {
    float[] array = new float[collection.size()];
    Iterator<Float> iterator = collection.iterator();
    int i = 0;//from w w w.  j  a v  a2  s  .  c  o  m
    while (iterator.hasNext()) {
        array[i++] = iterator.next();
    }
    return array;
}

From source file:Main.java

private static <T extends Comparable<T>> T getExtremum(Collection<T> vals, int comparator) {
    if (vals.size() == 0)
        return null;
    Iterator<T> it = vals.iterator();
    T ret = it.next();//from   w  w w.ja  v a2s  .  c  om
    while (it.hasNext()) {
        T v = it.next();
        if (v.compareTo(ret) * comparator > 0)
            ret = v;
    }
    return ret;
}

From source file:Main.java

public static short[] asShortArray(Collection<Short> collection) {
    short[] array = new short[collection.size()];
    Iterator<Short> iterator = collection.iterator();
    int i = 0;//w ww.  ja v a  2s.  c  o  m
    while (iterator.hasNext()) {
        array[i++] = iterator.next();
    }
    return array;
}

From source file:Main.java

public static int[] asIntArray(Collection<Integer> collection) {
    int[] array = new int[collection.size()];
    Iterator<Integer> iterator = collection.iterator();
    int i = 0;/*from w  w w  . ja  v a2  s .c om*/
    while (iterator.hasNext()) {
        array[i++] = iterator.next();
    }
    return array;
}

From source file:Main.java

/**
 * Creates new collection that contains only element whose string
 * representation starts with prefix It uses raw collection - not
 * parameterized. There is no {@link SuppressWarnings} annotation so IDE
 * will probably report rawtype and/or unchecked warnings.
 * //  w  ww  .jav  a 2  s  . c o m
 * @param c
 *            raw collection to create prefixed collection from
 * @param prefix
 *            to use as filter
 * @return collection without nulls
 */
public static Collection withPrefixRaw(Collection c, String prefix) {
    Collection prefixed = new ArrayList();
    Iterator iterator = c.iterator();
    while (iterator.hasNext()) {
        Object o = iterator.next();
        if (o != null && o.toString().startsWith(prefix)) {
            prefixed.add(o);
        }
    }
    return prefixed;
}

From source file:Main.java

/**
 * Returns the first object of given type or its subtypes
 * @param clazz The class you want to return the first instance found of.
 * @param objects The collection you'd like to search.
 * @return The first instance of a specific Class found in a Collection.  Returns null if no instance is found.
 *///from  w w w . j  a  va2s .  c  om
public static Object getFirstInstance(Class clazz, Collection objects) {
    if (objects != null && clazz != null) {
        Iterator objectsIterator = objects.iterator();
        while (objectsIterator.hasNext()) {
            Object instance = objectsIterator.next();
            if (clazz.isAssignableFrom(instance.getClass())) {
                return instance;
            }
        }
    }
    return null;
}