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:com.mseeworld.qzh.util.Debug.java

/**
 * ??//from   ww  w.  j av a  2  s.co m
 * @param list
 * @param ts
 * @date    2013-5-8
 */
public static final <T> void printf(Collection<T> list, ToString<T> ts) {
    if (list == null || list.isEmpty())
        return;
    Iterator<T> iter = list.iterator();
    while (iter.hasNext()) {
        T t = iter.next();
        //          if(t == null)continue ;
        if (ts == null) {
            System.out.println(t.toString());
        } else {
            String msg = ts.toString(t);
            //              if(msg != null) {
            System.out.println(msg);
            //              }
        }
    }
}

From source file:com.safetys.framework.jmesa.util.ItemUtils.java

/**
 * Get the Class for the property.//  www.  ja v a  2s  .  co  m
 * 
 * @param items The Collection of Beans or Maps.
 * @param property The Bean attribute or Map key.
 * @return The Class for the property.
 */
public static Class<?> getPropertyClassType(Collection<?> items, String property) throws Exception {

    Object item = items.iterator().next();

    if (item instanceof Map) {
        for (Object object : items) {
            Map<?, ?> map = (Map<?, ?>) object;
            Object val = map.get(property);

            if (val == null) {
                continue;
            }

            return val.getClass();
        }
    }

    Class<?> type = null;
    try {
        type = PropertyUtils.getPropertyType(item, property);
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Had problems getting property type by object, trying reflection...");
        }
        type = BeanUtils.getPropertyType(item, property);
    }
    return type;
}

From source file:info.magnolia.cms.beans.config.Listener.java

/**
 * Cache listener content from the config repository.
 *//*from  w  w w  .j  a v  a 2 s  .com*/
private static void cacheContent(Collection listeners) {

    Iterator ipList = listeners.iterator();
    while (ipList.hasNext()) {
        Content c = (Content) ipList.next();
        try {
            Map types = new Hashtable();
            Listener.cachedContent.put(c.getNodeData("IP").getString(), types); //$NON-NLS-1$
            Iterator it = c.getContent("Access").getChildren().iterator(); //$NON-NLS-1$
            while (it.hasNext()) {
                Content type = (Content) it.next();
                types.put(type.getNodeData("Method").getString().toLowerCase(), StringUtils.EMPTY); //$NON-NLS-1$
            }
        } catch (RepositoryException re) {
            log.error("RepositoryException caught while loading listener configuration: " + re.getMessage(), //$NON-NLS-1$
                    re);
        }
    }
}

From source file:melnorme.utilbox.tests.CommonTestExt.java

public static <T, PRED extends Visitor<T>> void visitContainer(Collection<T> coll, PRED... predicates) {
    Iterator<T> iterator = coll.iterator();
    assertTrue(coll.size() == predicates.length);
    for (int i = 0; iterator.hasNext(); i++) {
        T next = iterator.next();/*w ww  . j av a2  s  . c  om*/
        predicates[i].visit(next);
    }
}

From source file:Main.java

private static String internalJoin(String sep, Collection<Object> pieces) {
    StringBuilder sb = new StringBuilder();
    boolean skipSep = true;
    Iterator<Object> iter = pieces.iterator();
    while (iter.hasNext()) {
        if (skipSep) {
            skipSep = false;/*  ww w. j  av  a  2  s .  c o  m*/
        } else {
            sb.append(sep);
        }

        Object obj = iter.next();
        if (obj == null) {
            sb.append("null");
        } else {
            sb.append(obj.toString());
        }
    }
    return sb.toString();
}

From source file:Main.java

public static <T> List<T> search(final String str, Collection<? extends T> coll, Comparable<? super T> comp) {
    if (comp == null)
        return null;
    List<T> list = new ArrayList<T>();
    Iterator<? extends T> i = coll.iterator();
    while (i.hasNext()) {
        T next = i.next();/*from   w  ww.  j a  v  a 2s  . com*/
        if (comp.compareTo(next) > 0) {
            list.add(next);
        }
    }
    return list;
}

From source file:Main.java

/**
 * Returns an iterator that returns up to max number of elements from the
 * Collection/*from w w  w. j a v a2s.  c  o  m*/
 */
private static <T> Iterator<T> iterator(final Collection<T> c, final int count) {
    return new Iterator<T>() {

        private final Iterator<T> it = c.iterator();

        private int item = 0;

        public boolean hasNext() {
            if (item >= count) {
                return false;
            }
            return it.hasNext();
        }

        public T next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            item++;
            return it.next();
        }

        public void remove() {
            it.remove();
            item--;
        }
    };
}

From source file:Main.java

public static Integer firstUnique(Collection<Integer> collection) {
    Map<Integer, Integer> linkedHashMap = new LinkedHashMap<Integer, Integer>();
    java.util.Iterator<Integer> it = collection.iterator();
    Integer currentInt;// w ww  . ja  va  2  s  . co  m
    java.util.Iterator it1;
    Set entrySet;
    Map.Entry<Integer, Integer> me;

    while (it.hasNext()) {
        currentInt = it.next();
        if (!linkedHashMap.containsKey(currentInt)) {
            linkedHashMap.put(currentInt, 1);
        } else {
            linkedHashMap.put(currentInt, linkedHashMap.get(currentInt) + 1);
        }
    }

    entrySet = linkedHashMap.entrySet();
    it1 = entrySet.iterator();
    while (it1.hasNext()) {
        me = (Entry) it1.next();
        if (me.getValue().equals(1)) {
            return (Integer) me.getKey();
        }
    }

    return null;
}

From source file:org.springframework.cloud.vault.VaultErrorMessage.java

/**
 * Obtain the error message from a JSON response.
 * /*from  www  .  j a va  2s .c o  m*/
 * @param json
 * @return
 */
static String getError(String json) {

    if (json.contains("\"errors\":")) {

        try {
            Map<String, Object> map = OBJECT_MAPPER.readValue(json.getBytes(), Map.class);
            if (map.containsKey("errors")) {

                Collection<String> errors = (Collection<String>) map.get("errors");
                if (errors.size() == 1) {
                    return errors.iterator().next();
                }
                return errors.toString();
            }

        } catch (IOException o_O) {
            // ignore
        }
    }
    return json;
}

From source file:Main.java

public static <T> Map<T, Integer> getCardinalityMap(final Collection<T> coll) {
    if (coll == null) {
        return null;
    }//from   ww w . j  av  a 2s  .  c om

    Map<T, Integer> result = new HashMap<T, Integer>();
    Iterator<T> it = coll.iterator();
    while (it.hasNext()) {
        T t = it.next();
        Integer count = result.get(t);
        if (count == null) {
            result.put(t, ONE);
        } else {
            result.put(t, new Integer(count.intValue() + 1));
        }
    }
    return result;
}