What is an Iterator?
Iterator is an interface, which is found in java.util package. It provides a way to access elements of a collection sequentially without exposing its underlying implementation.
The iterator allows the traversal of elements and supports removing elements during iteration. The Iterator interface has three main methods:
- boolean hasNext(): Returns true if there are more elements in the collection.
- E next(): Returns the next element in the collection.
- void remove(): Removes the last element returned by next() from the collection (optional operation).