|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface IterableIterator<E>
An Iterator
, which can itself be the subject of an enhanced for
statement.
This comes in handy if you want to supply more than one iterator for a class. Since only one
Iterator
can be retrieved by the iterator()
method, other methods have to
be provided for the other iterators. But then, those iterators cannot be used with an enhanced for
statement, because that statement relies on invocation of the iterator()
method. Therefore, those
iterators have to be Iterable
themselves.
This interface does not introduce new methods. It just unifies Iterator
and Iterable
.
Normally, your custom IterableIterator
would just return itself in its additional iterator()
method, like this:
class CustomList<E> implements List<E> { private List<E> backend = new ArrayList<E>(); private class CustomIterator<E> implements IterableIterator<E> { /* * implement hasNext(), next() and remove() in your custom fashion */ /* make this Iterator iterable */ public Iterator<E> iterator() { return this; // just return ourselves } } ... /* this returns the normal iterator */ public Iterator<E> iterator() { return backend.iterator(); // use ArrayList's iterator } /* this returns our custom iterator */ public IterableIterator<E> custom() { return new CustomIterator<E>(); } } CustomList<Foo> foos = new CustomList<Foo>(); ... add elements ... // iterate over all elements: for (Foo foo: foos) { ... } // iterate over custom elements: for (Foo foo: foos.custom()) { ... }
Ceterum censeo HTML in Javadoc is the dumbest idea ever.
Method Summary |
---|
Methods inherited from interface java.util.Iterator |
---|
hasNext, next, remove |
Methods inherited from interface Iterable |
---|
iterator |
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |