An iterator is not a collection, but rather a way to access the elements of a collection one by one. The two basic operations on an iterator it
are next
and hasNext
. A call to it.next()
will return the next element of the iterator and advance the state of the iterator. Calling next
again on the same iterator will then yield the element one beyond the one returned previously. If there are no more elements to return, a call to next
will throw a NoSuchElementException
. You can find out whether there are more elements to return using Iterator’s hasNext
method.
This is a companion discussion topic for the original entry at http://docs.scala-lang.org//overviews/collections/iterators