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 boolean isEmptyCollection(Collection<?> cols) {
    if (cols == null || cols.isEmpty()) {
        return true;
    }/*  w w w  .  jav  a2  s . co  m*/
    Iterator<?> ite = cols.iterator();
    while (ite.hasNext()) {
        if (ite.next() == null) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static <K> Object firstElementOrNull(Iterable iterable) {
    Iterator iterator = iterable.iterator();
    if (iterator.hasNext()) {
        return iterator.next();
    } else {//from   w  ww  . j  a va2s  .c om
        return null;
    }
}

From source file:Main.java

public static <T, C extends Collection<? super T>> C addTo(final Iterator<T> iter, final C c) {
    while (iter.hasNext())
        c.add(iter.next());
    return c;/*  ww  w  .j a v  a 2s .c o m*/
}

From source file:Main.java

/**
 * Add all items in iterator to target collection
 * @param <T>/*from   w ww .j a  v a  2 s.c  om*/
 * @param <U>
 * @param source
 * @param target
 * @return
 */
public static <T, U extends Collection<T>> U addAll(Iterator<T> source, U target) {
    while (source.hasNext()) {
        target.add(source.next());
    }
    return target; // for chaining
}

From source file:Main.java

/**
 * Adds all of the items from a given {@code Iterator} into a
 * {@code Collection}./*from   w  w w.  j  a  v a  2  s  .  c  om*/
 *
 * @param <T>
 *            the element type of the given {@code Iterator} and
 *            {@code Collection}
 * @param <C>
 *            the type of the given {@code Collection}
 * @param c
 *            the {@code Collection} to add items to
 * @param i
 *            the {@code Iterator} to take items from
 * @return {@code c}
 */
public static <T, C extends Collection<T>> C fillFromIterator(C c, Iterator<T> i) {
    while (i.hasNext()) {
        c.add(i.next());
    }

    return c;
}

From source file:Main.java

/**
 * Returns an arbitrary element from the collection
 *//*from w  w w  .j a v  a  2 s  . c o  m*/
public static <T> T getAnElement(Collection<T> coll) {
    if (coll == null) {
        throw new IllegalArgumentException("Called with null collection");
    } else if (coll.size() == 0) {
        return null;
    }
    if (coll instanceof List) {
        return ((List<T>) coll).get(0);
    }
    Iterator<T> it = coll.iterator();
    return it.next();
}

From source file:Main.java

/**
 * Subtract objects in collection b from collection a. The string value
 * of the object is used for comparisons.
 * //from ww  w . ja  va2s  .  co  m
 * @param a a collection that will have members removed
 * @param b a collection whose objects will be removed from a
 * @return a new collection that contains the remaining objects in a
 */
public static Collection subtractByString(Collection a, Collection b) {
    Collection retColl = new ArrayList();
    Object obj = null;
    Iterator it = a.iterator();
    while (it.hasNext()) {
        obj = it.next();
        if (!b.contains(obj)) {
            retColl.add(obj);
        }
    }
    return retColl;
}

From source file:Main.java

public static String getPrettyString(Collection<?> coll) {
    StringBuffer sb = new StringBuffer();
    Iterator<?> it = coll.iterator();

    while (it.hasNext()) {
        String s = it.next().toString();
        sb.append(s + "\n");
    }/*from w w  w. ja v a2 s  .com*/

    return sb.toString().trim();
}

From source file:Main.java

/**
 * Parse Set to ArrayList//  w  w w  . j  av  a 2  s  .c o m
 * 
 * @param set
 * @return ArrayList
 */
public static <T> ArrayList<T> setToArrayList(Set<T> set) {
    if (set == null)
        return null;
    ArrayList<T> tArray = new ArrayList<T>();
    Iterator<T> it = set.iterator();
    while (it.hasNext()) {
        T t = it.next();
        tArray.add(t);
    }
    return tArray;
}

From source file:Main.java

public static <T> List<T> subtract(Collection<T> a, Collection<T> b) {
    ArrayList list = new ArrayList(a);
    Iterator i$ = b.iterator();

    while (i$.hasNext()) {
        Object element = i$.next();
        list.remove(element);/* w  w w.j a va2  s.c o  m*/
    }

    return list;
}