Example usage for java.util Iterator hasNext

List of usage examples for java.util Iterator hasNext

Introduction

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

Prototype

boolean hasNext();

Source Link

Document

Returns true if the iteration has more elements.

Usage

From source file:Main.java

public static String Map2StringPrettyPrint(Hashtable m) {
    String retString = "";
    Iterator iter = m.keySet().iterator();

    while (iter.hasNext()) {
        String key = (String) iter.next();
        retString += key + " = " + m.get(key) + "\n";
    }/*from ww w.  j  a va  2 s  . co  m*/

    return (retString.trim());
}

From source file:Main.java

public static Collection and(Collection collection1, Collection collection2) {
    Vector completeList = new Vector();

    if (collection2 == null)
        return completeList;
    Iterator i = collection2.iterator();
    while (i.hasNext()) {
        Object addlItem = i.next();
        if (collection1.contains(addlItem) == true) {
            completeList.addElement(addlItem);
        }/*w w w  .  java2s.c  om*/
    }
    return completeList;
}

From source file:Main.java

/**
 * Adds the contents of an iterator to an existing Collection.
 * @param <T> The type of elements returned by the iterator.
 * @param collection The receptacle to put the elements into.
 * @param elementsToAdd The elements to add to the Collection.
 *///from w  ww  . j a v  a  2 s.c o  m
public static <T> void addToCollection(Collection<T> collection, Iterator<T> elementsToAdd) {
    while (elementsToAdd.hasNext()) {
        T next = elementsToAdd.next();
        collection.add(next);
    }
}

From source file:Main.java

public static List toList(Set set) {
    if (set == null) {
        return null;
    }// w w  w . ja  va 2s  .  co  m
    if (set.size() == 0)
        return new ArrayList();
    List list = new ArrayList();
    Iterator itor = set.iterator();
    while (itor.hasNext()) {
        Object obj = itor.next();
        list.add(obj);
    }
    return list;
}

From source file:Main.java

public static <T> void addArrayListToArrayList(ArrayList<T> array, ArrayList<T> arrayList) {
    Iterator<T> it = array.iterator();
    while (it.hasNext()) {
        arrayList.add(it.next());//from ww w.j a v a  2 s .c  o  m
    }
}

From source file:Main.java

public static <E> void addAll(Collection<E> collection, Iterator<? extends E> iterator) {
    while (iterator.hasNext()) {
        collection.add(iterator.next());
    }/*from ww  w . j  a va2  s.c  o  m*/
}

From source file:Main.java

public static <E> String toString(Iterator<E> iterator) {
    if (iterator == null || !iterator.hasNext()) {
        return "[]";
    }//from   www  .j  a va 2s .  co  m
    StringBuilder builder = new StringBuilder();
    builder.append('[');
    while (iterator.hasNext()) {
        builder.append(iterator.next()).append(',').append(' ');
    }
    return builder.append(']').toString();
}

From source file:Main.java

public static String nextOptionalArg(Iterator<String> argsIter, String defaultValue) {
    return argsIter.hasNext() ? argsIter.next() : defaultValue;
}

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 . j  a v a 2s.  c o 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

/**
 * Adds all of the items from a given {@code Iterator} into a
 * {@code Collection}.//from   w  ww . j  av  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;
}