- Forward


The Iterator Pattern
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • Computers vs. Calculators:
    • Computers can perform the same operation many times
  • Programming Languages:
    • Harness this power using loops
  • A Problem:
    • Traditional looping requires an understanding of the structure of the "aggregate"
Looping and Aggregates
Back SMYC Forward

Looping Over an Array

javaexamples/iterator/ArrayExample.java (Fragment: 1)
 
Looping and Aggregates (cont.)
Back SMYC Forward

Looping Over an ArrayList

javaexamples/iterator/ArrayListExample.java (Fragment: 1)
 
Looping and Aggregates (cont.)
Back SMYC Forward

Looping Over a Linked Structure

javaexamples/iterator/LinkedListExample.java (Fragment: 1)
 
Some Observations
Back SMYC Forward
  • Applications often "loop" over the same aggregate object in many classes and methods
  • Changing from one aggregate to another is, as a result, very inconvenient
  • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure
Important Operations
Back SMYC Forward
  • Reset its "pointer" (or cursor) to the first element.
  • Determine if there are any more elements in the sequence.
  • Move its "pointer" to the next element.
  • Retrieve the "current" element.
The Iterator Pattern
Back SMYC Forward

In UML:

iterator
Examples in Java
Back SMYC Forward
  • The Aggregates:
    • Classes that realize the Iterable interface (including classes that implement the List interface like ArrayList and classes that realize the Set interface like HashSet )
  • The Iterator:
    • Classes that realize the Iterator interface
Examples in Java (cont.)
Back SMYC Forward
collections_java_parameterized

Note that the Map interface does not extend Iterable or Collection, but one can get a Set or Collection object from a Map.

Benefits of the Iterator
Back SMYC Forward

What code must change to use a HashSet instead of an ArrayList?

Iterable<String> cities; cities = new ArrayList<String>(); // Important code // An example loop Iterator<String> i = cities.iterator(); while (i.hasNext()) { String city = i.next(); System.out.println(city); }
Benefits of the Iterator (cont.)
Back SMYC Forward
  • The Principle Benefit:
    • You can easily change the type of aggregate being used without having to change any loops (and, if you declare variables properly, without having to change any of the declarations, only the instantiations)
  • Other Benefits:
    • Several objects can be "looping" over the elements in the aggregate at the same time
    • A "filtered" list (e.g., names starting with the letter "A") is handled in exactly the same way that the "unfiltered" version is
The "New" Loop
Back SMYC Forward
  • The Idea:
    • Include a control structure that takes advantage of the Iterator Pattern (i.e., create a "for each" loop for any Object that implements the Iterable interface)
  • Syntax:
    • for (Class element : Iterable) statement
  • Example:
    • Iterable<String> cities; cities = new ArrayList<String>(); // Important code // An example loop for (String city: cities) { System.out.println(city); }
There's Always More to Learn
Back -