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 Map groupMap(Collection collection, String keyName) {
    Map map = new HashMap();
    if (collection == null || collection.isEmpty()) {
        return map;
    }// ww  w  .j  a  va  2s  .com

    Map eachMap = null;
    Object key = null;
    List groupList = null;

    Iterator iter = collection.iterator();

    while (iter.hasNext()) {
        eachMap = (Map) iter.next();
        key = eachMap.get(keyName);
        if (key == null) {
            continue;
        }
        if (map.containsKey(key)) {
            groupList = (List) map.get(key);
            groupList.add(eachMap);
        } else {
            groupList = new ArrayList();
            groupList.add(eachMap);
            map.put(key, groupList);
        }
    }
    return map;
}

From source file:Main.java

public static Map<Object, Object> collectionToMapUnsafe(Collection<?> keyCollection,
        Collection<?> valueCollection, Map<Object, Object> targetMap) {
    if (isEmpty(keyCollection)) {
        return targetMap;
    }/*from ww w.jav  a 2 s.com*/
    if (valueCollection == null)
        valueCollection = new ArrayList<>();

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

From source file:mitm.test.TestUtils.java

public static X509CRL loadX509CRL(File file) throws CertificateException, NoSuchProviderException,
        SecurityFactoryFactoryException, CRLException, FileNotFoundException {
    Collection<? extends CRL> crls = CRLUtils.readCRLs(file);

    if (crls.size() > 0) {
        return (X509CRL) crls.iterator().next();
    }//from   w ww  .  ja  va 2s . c  om

    return null;
}

From source file:Main.java

/**
 * Takes a collection and returns all elements assembled in
 * a {@link String} joined by the defined separator.
 * <br>/*  w  ww .ja va2s. c  o  m*/
 * Example: Create a {@link String} using a {@link List}
 * separated by "\n":
 * <br>
 * <code>
 * List list = new ArrayList();
 * <br>
 * list.add(1);
 * <br>
 * list.add(2);
 * <br>
 * list.add(3);
 * <br>
 * String output = CollectionUtils.join(list, "\n");
 * <br>
 * </code>
 * 
 * @param collection a {@link Collection} of objects to join.
 * @param separator the {@link String} separator used to join the collection. 
 * @return {@link String} joined string.
 */
public static String join(Collection<?> collection, String separator) {
    String output = "";
    if (collection != null) {
        Iterator<?> iterator = collection.iterator();
        while (iterator.hasNext()) {
            Object o = iterator.next();
            output += o;
            if (iterator.hasNext())
                output += separator;
        }
    }
    return output;
}

From source file:grails.plugin.searchable.internal.compass.mapping.CompassMappingUtils.java

/**
 * Get the mapping aliases for the given user-defined domain classes any
 * @param compass Compass instance/* w  w w  .j av a  2  s .  c  o m*/
 * @param clazzes the user-defined domain classes
 * @return the Compass aliases for the hierarchy
 */
public static String[] getMappingAliases(Compass compass, Collection clazzes) {
    Set aliases = new HashSet();
    for (Iterator iter = clazzes.iterator(); iter.hasNext();) {
        Class clazz = (Class) iter.next();
        aliases.add(getMappingAlias(compass, clazz));
    }
    return (String[]) aliases.toArray(new String[aliases.size()]);
}

From source file:de.zib.gndms.stuff.misc.LanguageAlgebra.java

/**
 * Get the largest prefix shared by a set of words.
 *
 * @param words The set of words to get the largest prefix from.
 * @return The greatest common prefix./*from   w w w .  j av  a2  s.  c  o m*/
 */
public static String getGreatestCommonPrefix(Collection<String> words) {
    if (null == words || 0 == words.size()) {
        throw new IllegalArgumentException(
                "Cannot build the greatest common prefix out of an empty set of words!");
    }

    String commonprefix = words.iterator().next();

    for (String w : words) {
        commonprefix = getCommonPrefix(commonprefix, w);
    }

    return commonprefix;
}

From source file:org.gc.networktester.util.Util.java

/**
 * Join the elements of the Collection into a String using the
 * provided separator, with a maximum elements. The type describes how
 * to retrieve the String representation of each element.
 *///from ww  w .j a  v a  2 s. c o m
public static <T> String join(Collection<T> col, String sep, int max, String ellipsizeText, String type) {
    StringBuilder sb = new StringBuilder();
    Iterator<T> li = col.iterator();
    if (li.hasNext()) {
        if (type.equals("toString")) {
            sb.append(li.next());
        } else if (type.equals("getClass")) {
            sb.append(li.next().getClass().getName());
        } else {
            sb.append("???");
        }
        while (li.hasNext()) {
            if (max == 1) {
                sb.append(ellipsizeText);
                return sb.toString();
            }
            sb.append(sep);
            if (type.equals("toString")) {
                sb.append(li.next());
            } else if (type.equals("getClass")) {
                sb.append(li.next().getClass().getName());
            } else {
                sb.append("???");
            }
            max--;
        }
    }
    return sb.toString();
}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.protegeOwl.CreateUtils.java

public static AssociationQualification createAssociationQualification(RDFProperty rdfProp,
        SupportedMappings lgSupportedMappings_) {
    String brText = rdfProp.getBrowserText();
    Collection labels = rdfProp.getLabels();
    AssociationQualification lgQual = createAssociationQualification(brText, rdfProp.getURI(),
            labels.isEmpty() ? brText : labels.iterator().next().toString(), lgSupportedMappings_);
    return lgQual;
}

From source file:Main.java

/**
 * @since 1.5//from   ww w . ja va  2 s.  c  om
 */
public static String toString(final Collection<?> c, final String sep) {
    if (c instanceof List) {
        return toString((List<?>) c, sep);
    }
    final int n = c.size();
    if (n <= 0) {
        return ""; //$NON-NLS-1$
    } else if (n == 1) {
        return c.iterator().next().toString();
    } else {
        final Iterator<?> iter = c.iterator();
        final StringBuilder sb = new StringBuilder(iter.next().toString());
        for (int i = 1; i < n; i++) {
            sb.append(sep);
            sb.append(iter.next().toString());
        }
        return sb.toString();
    }
}

From source file:de.ingrid.portal.security.util.SecurityHelper.java

/**
 * Get merged permissions from user and his roles.
 * /*from  w w  w  . ja  va2s.c om*/
 * @param p
 * @param roles
 * @param permissionManager
 * @return
 */
public static Permissions getMergedPermissions(Principal p, Collection<Role> roles,
        PermissionManager permissionManager) {

    Permissions result = null;
    Collection<Principal> principals = new ArrayList<Principal>();
    principals.add(p);
    Iterator<Role> roleIterator = roles.iterator();
    while (roleIterator.hasNext()) {
        // check for role based permission to show the user
        Role role = roleIterator.next();
        principals.add(role);
    }
    result = permissionManager.getPermissions(principals.toArray(new Principal[principals.size()]));
    return result;
}