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

ListfromIterator(Iterator iterator)
Create a list containing the elements of an iterator
List<Object> l = list();
while (iterator.hasNext()) {
    l.add(iterator.next());
return l;
SetfromIterator(Iterator itr)
from Iterator
Set<E> set = new HashSet<E>();
while (itr.hasNext()) {
    set.add(itr.next());
return set;
Tget(Iterator iterator, int position)
get
for (int i = 0; i < position; i++) {
    iterator.next();
return iterator.next();
intgetEnumeratedObjectCount(Iterator objects)
* Gets the enumerated object count.
int count = 0;
while (objects != null && objects.hasNext()) {
    @SuppressWarnings("unused")
    Object obj = objects.next();
    count++;
return count;
EnumerationgetEnumeration(Iterator i)
get Enumeration
Vector v = new Vector();
while (i.hasNext())
    v.addElement(i.next());
return v.elements();
EnumerationgetIteratorEnumeration(final Iterator i)
get Iterator Enumeration
return new Enumeration() {
    public boolean hasMoreElements() {
        return i.hasNext();
    public Object nextElement() {
        return i.next();
};
...
ObjectgetObjectAtIteratorPos(Iterator i, int pos)
Given a numeric position, cycle through the given Iterator until the position is reached, and return the Object found there.
int currPos = 0;
Object oc;
while (i.hasNext()) {
    oc = i.next();
    if (currPos == pos)
        return oc;
    else
        currPos++;
...
booleanhasNext(final Iterator i)
has Next
return (i != null) && i.hasNext();
booleanhasNext(final Iterator it)
Asks an iterator if there is next value.
return it != null && it.hasNext();
booleanhasNextIteration(final Iterator iterator)
has Next Iteration
if (iterator.hasNext()) {
    while (iterator.hasNext()) {
        iterator.next();
    return true;
} else {
    return false;