Java Utililty Methods Iterator

List of utility methods to do Iterator

Description

The list of methods to do Iterator are organized into topic(s).

Method

voidaddAllToSet(Set set, Iterator it)
add All To Set
while (it.hasNext()) {
    set.add(it.next());
CaddTo(final Iterator iter, final C c)
add To
while (iter.hasNext())
    c.add(iter.next());
return c;
voidappendTo(final Iterator iter, final List list)
Appends all elements which the iterator is willing to produce to the given list.
assert null != iter;
assert null != list;
while (iter.hasNext()) {
    list.add(iter.next());
booleanareEqual(final Iterator a, final Iterator b)
are Equal
while (a.hasNext() || b.hasNext()) {
    if (a.hasNext() != b.hasNext())
        return false;
    if (!a.next().equals(b.next()))
        return false;
return true;
booleanareEqual(final Iterator ittyA, final Iterator ittyB)
Checks if the contents of the two iterators are equal and of the same length.
if (ittyA.hasNext() != ittyB.hasNext())
    return false;
while (ittyA.hasNext()) {
    if (!ittyB.hasNext())
        return false;
    if (ittyA.next() != ittyB.next())
        return false;
return true;
T[]asArray(T[] arr, Iterator itr)
as Array
ArrayList<T> lst = new ArrayList<T>();
while (itr.hasNext()) {
    lst.add(itr.next());
Class tclass = arr.getClass().getComponentType();
return lst.toArray(arr);
ListasList(final Iterator iterator)
Converts a given iterator in a list.
final List<T> result = new ArrayList<T>();
while (iterator.hasNext()) {
    result.add(iterator.next());
return result;
ListasList(Iterator it)
as List
List<T> lst = new ArrayList<T>();
while (it.hasNext()) {
    lst.add(it.next());
return lst;
voidasStringOn(StringBuffer sb, Iterator iter, String separator)
Copies the elements returned by the iterator onto the string buffer each delimited by the separator.
if (!iter.hasNext())
    return;
sb.append(iter.next());
while (iter.hasNext()) {
    sb.append(separator);
    sb.append(iter.next());
Iteratorcast(Iterator p)
cast
return (Iterator<T>) p;