java.lang.Iterable
and java.lang.Iterator
are related interfaces commonly seen in Java. An iterable is simply an object which can be iterated over. An iterator is the object responsible for this iteration.
This exercise will focus on creating your own iterator, OhMyIterator
. Find java.lang.Iterator
in Eclipse (hot key <Shift-Ctrl-T>
) and use the Javadoc view to read its documentation.
There are only a few methods that need to be implemented: next()
, hasNext()
, and remove()
. The tests for these methods have been provided. Your assignment is to make them pass. Many iterators do not support removing elements, so our iterator will throw UnsupportedOperationException
if the remove()
method is called. (If you are using Java 8, you won't need to implement remove()
, since the default implementation provided will satisfy the test.)
This interface only requires one method to be implemented, iterator()
, which returns an Iterator
instance.
Create your own class that implements Iterable
and creates a new instance of OhMyIterator
whenever iterator()
is called.
- Remember the diamond operator.
- Iterators and iterables are common topics for interview questions. Know the difference. Same goes for other Java Collections Framework classes like
java.util.Collection
,java.util.List
, andjava.util.Map
.