Example usage for java.util Iterator next

List of usage examples for java.util Iterator next

Introduction

In this page you can find the example usage for java.util Iterator next.

Prototype

E next();

Source Link

Document

Returns the next element in the iteration.

Usage

From source file:Main.java

public static String join(final Collection<?> collection, final String separator) {
    if (collection == null || collection.size() == 0) {
        return null;
    }/*from w ww. j a  v a 2s  .c  o m*/
    StringBuilder sb = new StringBuilder(collection.size() * 7);
    Iterator<?> iterator = collection.iterator();
    sb.append(iterator.next());
    while (iterator.hasNext()) {
        sb.append(separator);
        sb.append(iterator.next());
    }
    return sb.toString();
}

From source file:Main.java

public static String toString(Iterable<?> it, String separator) {
    final StringBuilder builder = new StringBuilder();
    final Iterator<?> iterator = it.iterator();
    while (iterator.hasNext()) {
        builder.append(iterator.next().toString());
        if (iterator.hasNext()) {
            builder.append(separator);//from ww  w  .j av  a  2  s . c o  m
        }
    }
    return builder.toString();
}

From source file:Main.java

public static <E> List<E> iteratorToList(Iterator<E> iterator) {
    List<E> list = new ArrayList<E>();
    while (iterator.hasNext()) {
        list.add(iterator.next());
    }/* w  w  w.  j a v  a  2 s .c  o  m*/
    return list;
}

From source file:Main.java

public static <T> boolean doCompare(Collection<T> c1, Collection<T> c2) {

    if (c1 == null && c2 == null) {
        return true;
    }/*from w  w w . j av a2  s  .  co m*/

    if (c1 == null || c2 == null) {
        return false;
    }

    if (c1.size() != c2.size()) {
        return false;
    }

    Iterator<T> it = c1.iterator();
    while (it.hasNext()) {
        if (!c2.contains(it.next())) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

static void appendJson(JSONObject json, JSONObject jsonToAppend) {
    if (jsonToAppend == null)
        return;/*from  w ww  .  j ava  2  s  .co m*/

    try {
        Iterator<String> it = jsonToAppend.keys();
        while (it.hasNext()) {
            String key = it.next();
            json.put(key, jsonToAppend.get(key));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Constructs a SortedSet&lt;T> with all the elements available from i
 * @param i an iterator to pull elements from
 * @param <T> The type of the elements
 * @return a SortedSet of the elements//from www .  j ava  2  s.c  o  m
 */
public static <T> SortedSet<T> sortedSetFromIterator(Iterator<T> i) {
    SortedSet<T> retval = new TreeSet<T>();
    while (i.hasNext())
        retval.add(i.next());
    return retval;
}

From source file:Main.java

public static final <K, V> void printM(Map<K, V> map, String sep) {
    Iterator<Entry<K, V>> it = map.entrySet().iterator();
    while (it.hasNext()) {
        Entry<K, V> e = it.next();
        System.out.append(e.getKey().toString()).append("::").append(e.getValue().toString());
        if (it.hasNext()) {
            System.out.append(sep);
        }/*www  .j  av a 2 s  .  c om*/
    }

}

From source file:Main.java

public static <E> E first(Collection<E> collection) {

    if (isEmpty(collection)) {
        return null;
    }//from  w  w w . j  a va 2  s .  c o m

    Iterator<E> iterator = collection.iterator();

    if (iterator.hasNext()) {
        return iterator.next();
    }

    throw new IllegalStateException("Should not happen");
}

From source file:Main.java

public static int cardinality(java.lang.Object obj, Collection coll) {
    int numObject = 0;
    if (coll != null) {
        Iterator iter = coll.iterator();

        while (iter.hasNext()) {
            Object collObj = iter.next();

            if (collObj != null) {
                if (collObj.equals(obj)) {
                    numObject++;/*from w  ww .j a v  a2s  .  c  o  m*/
                }
            }

        }
    }
    return numObject;
}

From source file:Main.java

public static void printMap(Map<String, String> map) {
    Set<Map.Entry<String, String>> s = map.entrySet();
    Iterator<Map.Entry<String, String>> it = s.iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
        String key = (String) entry.getKey();
        String value = (String) entry.getValue();
        System.out.println(key + " => " + value);
    }/*from  www.  ja  v a 2  s  .  com*/
}