Iterator Interface
Package: java.util
An iterator is a special type of object whose sole purpose in life is to let you step through the elements of a collection. If you want to build a class that can be processed by the Java enhanced for statement (also known as “foreach”), the class must implement the Iterator class and provide an iterator method, which in turn must return an object that implements the Iterator class. This class must implement each of the three methods listed in the following table.
Methods
|
Method |
Explanation |
|
|
Returns |
|
|
Returns the next element in the collection. |
|
|
Removes the most recently retrieved element. |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access