Iterating a Collection

The collections framework introduced the concept of iterators to take the place of the Enumeration interface for traversing the elements of a collection. The iterators are defined using interfaces just like Enumeration, but they differ in their method names, and they optionally allow you to remove elements from a collection while it is being iterated.

The Iterator Interface

An Iterator can be obtained for any object that implements the Collection interface. This interface defines three methods for traversing a collection:

boolean hasNext()
Object next()
void remove()

These methods allow you to check the iteration for more elements, retrieve the next element, and remove the last retrieved element from the collection. The ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.