28 Iterator

WHEN YOU EXTEND a codebase by adding a new type of collection, you may find that you need to extend your extension by adding an iterator. This chapter examines the particular case of iterating over a composite. In addition to iterating over new types of collections, the topic of iterating in a multithreaded environment brings up a number of interesting problems that warrant review. Simple as it seems, iteration is not a completely solved problem.

The intent of the ITERATOR pattern is to provide a way to access the elements of a collection sequentially.

Ordinary Iteration

Java has several approaches to iteration:

•   For, while, and repeat loops, typically using integer indexes

•   The Enumeration class (in java.util ...

Get Design Patterns in Java™, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.