6 Iteration Abstraction

This chapter discusses our final abstraction mechanism, the iteration abstraction, or iterator for short. Iterators are a generalization of the iteration mechanisms available in most programming languages. They permit users to iterate over arbitrary types of data in a convenient and efficient way.

For example, an obvious use of a set is to perform some action for each of its elements:

for all elements of the set    do action

Such a loop might go through the set completely—for example, to sum all elements of a set. Or it might search for an element that satisfies some criterion, in which case the loop can stop as soon as the desired element has been found.

IntSets as we have defined them so far provide no convenient way ...

Get Program Development in Java: Abstraction, Specification, and Object-Oriented Design 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.