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

/**
 * Joins the {@link String} representation of elements of a given list,
 * dividing with a separator./*from  w ww.  j a  va 2  s  .  co  m*/
 * 
 * @param input
 *            - the {@link List} of elements to join.
 * @param separator
 *            - the separator to place between elements
 * @return - the joined {@link String}
 */
public static <E extends Object> String join(List<E> input, String separator) {
    StringBuilder result = new StringBuilder();
    Iterator<E> it = input.iterator();

    while (it.hasNext()) {
        result.append(it.next());
        if (separator != null && it.hasNext()) {
            result.append(separator);
        }
    }

    return result.toString();
}

From source file:Main.java

public static <E> boolean contains(Collection<E> c, E item, BiPredicate<? super E, ? super E> equalTester) {
    Iterator<E> iter = c.iterator();
    while (iter.hasNext()) {
        if (equalTester.test(iter.next(), item)) {
            return true;
        }/*from ww  w.j  a va 2 s.c  om*/
    }
    return false;
}

From source file:Main.java

public static void printMap(Map map) {
    Iterator iter = map.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        System.out.println(entry.getKey() + ": " + entry.getValue());
    }// ww  w . j a v a2  s.  c o m
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static List<String> keysToList(Map<?, ?> map) {
    List<String> list = new ArrayList<String>();
    Set<?> set = map.entrySet();
    Iterator<?> iter = set.iterator();
    while (iter.hasNext()) {
        Map.Entry m = (Map.Entry) iter.next();
        list.add("" + m.getKey());
    }/*from www.  j  av  a2 s .  c  o m*/
    return list;
}

From source file:Main.java

public static void printMapBySet(Map map) {
    Set<String> ketSet = map.keySet();
    Iterator<String> iterator = ketSet.iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        String value = (String) map.get(key);
        Log.i(TAG, "----key = " + key + ",value = " + value);
    }/*w  w w .ja  va2 s  .  c  om*/

}

From source file:Main.java

public static <T> T first(Collection<T> c) {
    if (isEmpty(c))
        return null;

    if (c instanceof List)
        return (T) ((List<T>) c).get(0);

    Iterator<T> iter = c.iterator();
    if (iter.hasNext()) {
        return iter.next();
    }/* w  w  w  .ja va2s.co  m*/
    return null;
}

From source file:Main.java

/**
 * Converts the given collection to a <code>java.util.List</code>.
 * @param collection The collection to convert to a list.
 * @return A <code>java.util.List</code> containing all of the elements from the
 *         supplied collection./*from  w w w  .  j a  v  a 2s.  c o  m*/
 */
public static List toList(Collection collection) {
    if (collection instanceof List)
        return (List) collection;

    List list = new ArrayList();
    Iterator iterator = collection.iterator();
    while (iterator.hasNext()) {
        list.add(iterator.next());
    }
    return list;
}

From source file:Main.java

/**
 * an alternative which returns slightly more granular info than boolean pass/fail.
 * @return the number of add operations which "succeed" (modify the collection)
 * @see Collection#addAll(Collection)//from   w  ww .  j  a  v  a2  s. c o m
 */
public static <E> int addAll(Collection<E> collection, Iterator<? extends E> add) {
    int retVal = 0;
    while (add.hasNext())
        if (collection.add(add.next()))
            ++retVal;
    return retVal;
}

From source file:Main.java

/**
 * Append a collection of strings and delimiter.
 *
 * @param c A collection of strings./*from  w w w . j  a  va2 s  . c  o  m*/
 * @param delimiter A delimiter string.
 * @return The new string.
 */
public static String join(Collection<String> c, String delimiter) {
    if (c == null || delimiter == null) {
        throw new IllegalArgumentException("Collections and delimiters given to join cannot be null!");
    }
    StringBuilder builder = new StringBuilder("");
    Iterator<String> iter = c.iterator();
    while (iter.hasNext()) {
        builder.append(iter.next());
        if (iter.hasNext()) {
            builder.append(delimiter);
        }
    }
    return builder.toString();
}

From source file:CollectionAll.java

static void traverse(Collection coll) {
    Iterator iter = coll.iterator();
    while (iter.hasNext()) {
        String elem = (String) iter.next();
        System.out.print(elem + " ");
    }/*w ww.  ja  v  a  2s  .  com*/
    System.out.println();
}