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:net.longfalcon.newsj.util.ArrayUtil.java

public static <T> String stringify(Collection<T> collection, String delimiter) {
    StringBuilder sb = new StringBuilder();
    Iterator<T> iterator = collection.iterator();
    while (iterator.hasNext()) {
        T next = iterator.next();// w  w  w . j  a v a2  s. c  o  m
        if (next instanceof Object[]) {
            Object[] nextArr = (Object[]) next;
            for (Object o : nextArr) {
                sb.append(o.toString()).append(",");
            }
            sb.append(delimiter);
        } else {
            sb.append(next.toString()).append(delimiter);
        }
    }
    return sb.toString();
}

From source file:Main.java

public static <V> V getRandom(Collection<V> collection) {

    if (collection.isEmpty()) {
        return null;
    }/*from  w w w. ja  v  a2  s.  c o m*/

    int randIdx = (int) Math.round(Math.random() * (collection.size() - 1));
    int count = 0;
    Iterator<V> iterator = collection.iterator();
    while (iterator.hasNext()) {
        V current = iterator.next();
        if (count == randIdx) {
            return current;
        }
        count++;
    }

    throw new RuntimeException("Shouldn't happen");
}

From source file:it.govpay.core.utils.PspUtils.java

private static byte[] getLogo(String subFolder, String codPsp) {
    try {//from  w w w .j  av  a2 s.  com
        Collection<File> logos = FileUtils.listFiles(
                new File(GovpayConfig.getInstance().getLogoDir() + File.pathSeparator + subFolder),
                new PrefixFileFilter(codPsp), null);
        File logo = logos.iterator().next();
        return IOUtils.toByteArray(new FileInputStream(logo));
    } catch (Throwable t) {
        return logoPsp;
    }
}

From source file:com.amazonaws.services.logs.connectors.kinesis.KinesisTransformer.java

private static <E> Collection<Collection<E>> partition(Collection<E> coll, int maxPerPartition) {
    List<Collection<E>> partitions = new LinkedList<Collection<E>>();
    Iterator<E> iter = coll.iterator();
    while (iter.hasNext()) {
        List<E> partition = new LinkedList<E>();
        ;// w  w w.  j av a  2 s . c  o m
        for (int j = 0; iter.hasNext() && j < maxPerPartition; j++) {
            partition.add(iter.next());
        }
        partitions.add(partition);
    }
    return partitions;
}

From source file:Main.java

public static boolean equalsSet(Collection c1, Collection c2) {
    boolean equals;
    if (c1 == null) {
        equals = (c2 == null);/*from ww w .  j a v a 2s .c o  m*/
    } else if (c2 == null) {
        equals = false;
    } else if (c1.size() == c2.size()) {
        equals = true;
        Iterator iterC1 = c1.iterator();
        while (equals && iterC1.hasNext()) {
            Object o1 = iterC1.next();
            equals = c2.contains(o1);
        }
    } else {
        equals = false;
    }
    return equals;
}

From source file:Main.java

/**
 * //from ww w  .  j  a  v  a  2s .  c o  m
 * @param collection
 * @return int[]
 */
public static boolean[] toBooleanArray(final Collection<Boolean> collection) {
    final boolean[] array = new boolean[collection == null ? 0 : collection.size()];

    if (collection != null) {
        int i = 0;

        for (Iterator<Boolean> iterator = collection.iterator(); iterator.hasNext();) {
            array[i++] = iterator.next().booleanValue();
        }
    }

    return array;
}

From source file:Main.java

private static String toString(Collection<int[]> arrays) {
    if (arrays == null || arrays.isEmpty()) {
        return "[]";
    }//from w w  w. j a  v  a2 s  .c  o  m
    StringBuilder buffer = new StringBuilder();
    buffer.append('[');
    Iterator<int[]> it = arrays.iterator();
    while (it.hasNext()) {
        buffer.append(Arrays.toString(it.next()));
        if (it.hasNext()) {
            buffer.append(", ");
        }
    }
    buffer.append(']');
    return buffer.toString();
}

From source file:Main.java

/**
 * Extract the lone element of the specified Collection.
 * @param <E> collection type//from w ww  .ja  v  a 2  s  .  c o m
 * @param collection to read
 * @return sole member of collection
 * @throws IllegalArgumentException if collection is null/empty or contains more than one element
 * @since 4.0
 */
public static <E> E extractSingleton(final Collection<E> collection) {
    if (collection == null || collection.size() != 1) {
        throw new IllegalArgumentException("Can extract singleton only when collection size == 1");
    }
    return collection.iterator().next();
}

From source file:com.cnksi.core.tools.utils.Collections3.java

/**
 * ?Collectioncollectionnull./*from w  w w .ja  v a  2 s.  c om*/
 */
public static <T> T getFirst(Collection<T> collection) {

    if (isEmpty(collection)) {
        return null;
    }

    return collection.iterator().next();
}

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

/**
 * Get the parent GrailsDomainClass for the given GrailsDomainClass, if it
 * exists in the given collection otherwise null
 *
 * @param grailsDomainClass the class whose parent to find
 * @param grailsDomainClasses the collection of possible parents
 * @return null if the given class has no parent or the parent is not in the collection
 *///from w w w .j a v  a  2  s  . com
public static GrailsDomainClass getSuperClass(GrailsDomainClass grailsDomainClass,
        Collection grailsDomainClasses) {
    Set candidates = new HashSet();
    for (Iterator iter = grailsDomainClasses.iterator(); iter.hasNext();) {
        GrailsDomainClass gdc = (GrailsDomainClass) iter.next();
        if (gdc.getSubClasses().contains(grailsDomainClass)) {
            candidates.add(gdc);
        }
    }
    if (candidates.isEmpty()) {
        return null;
    }
    while (candidates.size() > 1) {
        Set copy = new HashSet(candidates);
        for (Iterator iter = copy.iterator(); iter.hasNext();) {
            GrailsDomainClass supsup = (GrailsDomainClass) iter.next();
            boolean remove = false;
            for (Iterator iter2 = candidates.iterator(); iter2.hasNext();) {
                GrailsDomainClass sup = (GrailsDomainClass) iter2.next();
                if (supsup.getSubClasses().contains(sup)) {
                    remove = true;
                    break;
                }
            }
            if (remove) {
                candidates.remove(supsup);
                break;
            }
        }
    }
    return (GrailsDomainClass) candidates.iterator().next();
}