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

/**
 * Fold up a collection into a string./*from  w ww  . java 2 s  . co m*/
 */
public static String join(Collection a_collection, String a_sDelimiter)
/*
---------------------------------------------------------------
d1      17-Oct-2006      Adam Bones      Created 
-----------------------------------------------------------------
 */
{
    StringBuffer buffer = new StringBuffer();
    Iterator it = a_collection.iterator();
    while (it.hasNext()) {
        buffer.append(it.next());
        if (it.hasNext()) {
            buffer.append(a_sDelimiter);
        }
    }
    return buffer.toString();
}

From source file:net.ontopia.topicmaps.utils.KeyGenerator.java

protected static String makeScopeKey(Collection<TopicIF> scope) {
    Iterator<TopicIF> it = scope.iterator();
    String[] ids = new String[scope.size()];
    int ix = 0;/*w w  w . j  a  v a 2  s.  c  o  m*/
    while (it.hasNext()) {
        TopicIF theme = it.next();
        ids[ix++] = theme.getObjectId();
    }

    Arrays.sort(ids);
    return StringUtils.join(ids, " ");
}

From source file:net.openkoncept.vroom.VroomUtilities.java

public static JSONObject convertObjectToJSONObject(Object object) throws JSONException {
    JSONObject jo = new JSONObject();
    if (object instanceof Character || object instanceof String || object instanceof Boolean
            || object instanceof Short || object instanceof Integer || object instanceof Long
            || object instanceof Float || object instanceof Double || object instanceof java.util.Date
            || object instanceof java.math.BigDecimal || object instanceof java.math.BigInteger) {
        jo.put("value", getValueForJSONObject(object));
    } else if (object instanceof java.util.Map) {
        Map m = (Map) object;
        Iterator iter = m.keySet().iterator();
        while (iter.hasNext()) {
            String key = (String) iter.next();
            jo.put(key, getValueForJSONObject(m.get(key)));
        }/*www .  j  a  va  2s. com*/
    } else if (object instanceof Collection) {
        Collection c = (Collection) object;
        Iterator iter = c.iterator();
        JSONArray ja = new JSONArray();
        while (iter.hasNext()) {
            ja.put(getValueForJSONObject(iter.next()));
        }
        jo.put("array", ja);
    } else if (object != null && object.getClass().isArray()) {
        Object[] oa = (Object[]) object;
        JSONArray ja = new JSONArray();
        for (int i = 0; i < oa.length; i++) {
            ja.put(getValueForJSONObject(oa[i]));
        }
        jo.put("array", ja);
    } else if (object instanceof ResourceBundle) {
        ResourceBundle rb = (ResourceBundle) object;
        Enumeration e = rb.getKeys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            Object value = getValueForJSONObject(rb.getObject(key));
            jo.put(key, value);
        }
    } else if (object != null) {
        Class clazz = object.getClass();
        PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(object);
        for (int i = 0; i < pds.length; i++) {
            PropertyDescriptor pd = pds[i];
            String name = pd.getName();
            if (!"class".equals(name) && PropertyUtils.isReadable(object, pd.getName())) {
                try {
                    Object value = PropertyUtils.getProperty(object, name);
                    jo.put(name, getValueForJSONObject(value));
                } catch (Exception e) {
                    // Useless...
                }
            }
        }
    } else {
        jo.put("value", "");
    }
    return jo;
}

From source file:net.landora.video.utils.UIUtils.java

public static <T> T select(Collection<T> collection) {
    if (collection == null || collection.isEmpty()) {
        return null;
    } else {// w  w w .  ja v  a2s  . co m
        return collection.iterator().next();
    }
}

From source file:com.salesmanager.core.util.LocaleUtil.java

public static void setLocaleToEntityCollection(Collection<I18NEntity> coll, Locale locale) {
    if (coll != null) {
        Iterator i = coll.iterator();
        while (i.hasNext()) {
            I18NEntity entity = (I18NEntity) i.next();
            entity.setLocale(locale);//from  w  w  w .  j  ava 2  s  .  c  o  m
        }
    }
}

From source file:StringUtils.java

/**
 * Returns a string consisting of all members of a collection separated by the
 * specified string. The <code>toString</code> method of each collection
 * member is called to convert it to a string.
 * /*from   w w  w  . ja  v a  2  s  .c o  m*/
 * @param c
 *          a collection of objects
 * @param joinWith
 *          the string that will separate each member of the collection
 */
public static String join(Collection c, String joinWith) {
    if (c == null)
        return "";

    StringBuffer buf = new StringBuffer();
    boolean first = true;
    for (Iterator iter = c.iterator(); iter.hasNext();) {
        if (first)
            first = false;
        else if (joinWith != null)
            buf.append(joinWith);
        buf.append(iter.next().toString());
    }
    return buf.toString();
}

From source file:Main.java

/**
 * //from  www . j a va2 s .com
 * @param collection
 * @return double[]
 */
public static double[] toDoubleArray(final Collection<Double> collection) {
    final double[] array = new double[collection == null ? 0 : collection.size()];

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

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

    return array;
}

From source file:Main.java

public static <E extends Comparable<E>> E getSmallestNotNull(final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getSmallestNotNull((List<? extends E>) c);
    }//w w w.j av a 2s.  c o  m

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (element == null) {
            continue;
        }
        if ((result == null) || (element.compareTo(result) < 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:Main.java

public static <E extends Comparable<E>> E getGreatestNotNull(final Collection<? extends E> c) {
    if ((c instanceof List) && (c instanceof RandomAccess)) {
        return getGreatestNotNull((List<? extends E>) c);
    }// w  w w. j  a v  a  2s. c o  m

    final Iterator<? extends E> iterator = c.iterator();
    E result = iterator.next();
    E element;

    while (iterator.hasNext()) {
        element = iterator.next();
        if (element == null) {
            continue;
        }
        if ((result == null) || (element.compareTo(result) > 0)) {
            result = element;
        }
    }

    if (result == null) {
        throw new NoSuchElementException();
    }

    return result;
}

From source file:com.fjn.helper.common.util.StringUtil.java

/**
 * Listsplit// w w  w  .j  av  a  2 s. c  o  m
 * @param list
 * @param split
 * @return
 */
public static String collectionToString(Collection list, String split) {
    if (isEmpty(split)) {
        split = ", ";
    }
    StringBuilder buf = new StringBuilder();
    if (list != null && !list.isEmpty()) {
        Iterator iter = list.iterator();
        if (iter.hasNext()) {
            buf.append(iter.next());
        }
        while (iter.hasNext()) {
            buf.append(split).append(iter.next());
        }
    }
    return buf.toString();
}