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 <K, V> Map<K, V> convertMap(final Map<K, V> map, final Collection<K> keys,
        final Collection<V> values) {
    if (keys.size() != values.size()) {
        throw new IllegalArgumentException("Invalid Collection. Collection size is not equals.");
    }// w ww . j  a va2 s  .co m
    Iterator<V> valueIt = values.iterator();
    for (K key : keys) {
        map.put(key, valueIt.next());
    }
    return map;
}

From source file:Main.java

public static void trimCollection(Collection collection, int numberOfElements) {
    if (collection.size() < numberOfElements) {
        return;//from   www.jav  a2  s.c  om
    }

    numberOfElements = collection.size() - numberOfElements;
    Iterator it = collection.iterator();
    int counter = 0;

    while (it.hasNext()) {
        if (counter <= numberOfElements) {
            it.next();
            counter++;
        }
        it.remove();
    }
}

From source file:Main.java

public static Collection extractField(Collection in, String fieldName, Class type) throws SecurityException,
        NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Iterator<Object> it = in.iterator();
    boolean isBoolean = (type == Boolean.class || type == boolean.class);
    String methodName = (isBoolean) ? "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) //$NON-NLS-1$
            : "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); //$NON-NLS-1$
    LinkedList<Object> out = new LinkedList<Object>();

    while (it.hasNext()) {
        Object obj = it.next();/*from w  ww.  j a v  a2 s.  c  om*/
        Method m = obj.getClass().getMethod(methodName, new Class[] {});
        Object value2 = m.invoke(obj, null);
        out.add(value2);

    }
    return out;
}

From source file:Main.java

public static String toString(Collection<Integer> stack, String delimiterChars) {
    if (stack.isEmpty()) {
        return "";
    }//from   ww w.j a  v a2  s .  c  om
    if (stack.size() == 1) {
        return Integer.toString(stack.iterator().next());
    }
    StringWriter writer = new StringWriter();
    String delimiter = "";
    for (Integer item : stack) {
        writer.append(delimiter);
        writer.append(Integer.toString(item));
        delimiter = delimiterChars;
    }
    return writer.toString();
}

From source file:org.springmodules.cache.util.Reflections.java

private static int reflectionHashCode(Collection collection) {
    int hash = INITIAL_HASH;

    for (Iterator i = collection.iterator(); i.hasNext();) {
        hash = MULTIPLIER * hash + reflectionHashCode(i.next());
    }/*from w  w w  . jav  a 2 s .  c  o  m*/

    return hash;
}

From source file:grails.plugin.searchable.internal.util.GrailsDomainClassUtils.java

/**
 * Gets the GrailsDomainClass for the given user-defined clazz
 * @param clazz the user-defined domain class
 * @param grailsDomainClasses the collection of GrailsDomainClasses to look thru
 * @return the corresponding GrailsDomainClass
 *///from   w w w.java  2s .co  m
public static GrailsDomainClass getGrailsDomainClass(Class clazz, Collection grailsDomainClasses) {
    for (Iterator iter = grailsDomainClasses.iterator(); iter.hasNext();) {
        GrailsDomainClass grailsDomainClass = (GrailsDomainClass) iter.next();
        if (grailsDomainClass.getClazz().equals(clazz)) {
            return grailsDomainClass;
        }
    }
    return null;
}

From source file:edu.byu.nlp.util.Iterables2.java

/**
 * Provides the same functionality as guava's Iterables.partition() but without a couple of 
 * shortcomings: handles split size 0, and the second collection can be bigger or smaller 
 * than the first.//w w w  . j  a  v  a  2s  .c om
 */
@SuppressWarnings("unchecked")
public static <I> Iterable<? extends Collection<I>> partition(Collection<I> labeledData, int splitSize) {
    Iterator<I> itr = labeledData.iterator();
    Collection<I> first = Lists.newArrayList();
    for (int cnt = 0; itr.hasNext() && cnt < splitSize; cnt++) {
        first.add(itr.next());
    }
    Collection<I> second = Lists.newArrayList();
    while (itr.hasNext()) {
        second.add(itr.next());
    }
    return Lists.newArrayList(first, second);
}

From source file:net.ontopia.topicmaps.cmdlineutils.rdbms.RDBMSIndexTool.java

protected static void print(String prefix, Collection c) {
    Iterator iter = c.iterator();
    while (iter.hasNext()) {
        Object k = iter.next();/*from  www  .  j  ava  2  s .c om*/
        System.out.println(prefix + k);
    }
}

From source file:net.sourceforge.jcctray.utils.ObjectPersister.java

private static void saveCruiseImpls(FileWriter writer, Collection cruiseImpls) throws IOException {
    for (Iterator iterator = cruiseImpls.iterator(); iterator.hasNext();) {
        writer.write("      <cruiseImpl>" + ((Class) iterator.next()).getName() + "</cruiseImpl>\n");
    }//from www  .  j  av a2  s  .  c o m
}

From source file:Main.java

public static boolean hasUniqueObject(Collection collection) {
    if (isEmpty(collection)) {
        return false;
    } else {/* w  w w.  j  a  va  2  s  . c om*/
        boolean hasCandidate = false;
        Object candidate = null;
        Iterator it = collection.iterator();

        while (it.hasNext()) {
            Object elem = it.next();
            if (!hasCandidate) {
                hasCandidate = true;
                candidate = elem;
            } else if (candidate != elem) {
                return false;
            }
        }

        return true;
    }
}