CPSC 210
D7: Iterator Pattern
Teaching
Please
Evaluation
Learning Goals
- To apply the Iterator Design Pattern to a given problem
The Iterator Pattern
Image source: https://refactoring.guru/
Iterator: Motivation
I have a collection of elements I don't know much about; but I know that there should be a "next element".
I want to operate on the collection (e.g. print an element) while iterating over it
Intent: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying implementation
Hiding information?! Have we ever done that? 😜
We've been doing this!
...all term long...
for (Item next : collection) {
// do something with next
}
...where collection was e.g. an ArrayList, LinkedList, HashSet of objects of type Item
- This is just a short-hand syntax that was introduced in Java 5 (2004)
- Before that we had to write it like this
Iterator<Item> itr = collection.iterator();
while (itr.hasNext()) {
Item nextItem = itr.next();
// do something with nextItem
}
Iterator in Java
- For this code to compile, the collection must have:
Iterator<Item> itr = collection.iterator();
while (itr.hasNext()) {
Item nextItem = itr.next();
// do something with nextItem
}
Iterator<E> iterator();
- And the iterator must have:
boolean hasNext();
E next();
Iterator Hierarchy
returns new ArrayListIterator<E>()
returns new LinkedListIterator<E>()
returns new HashSetIterator<E>()
Iterable<E>
Iterator<E> iterator()
<<interface>>
Iterator<E>
boolean hasNext()
E next()
void remove()
<<interface>>
Collection
<<interface>>
List<E>
<<interface>>
LinkedList<E>
Iterator<E> iterator()
ArrayList<E>
Iterator<E> iterator()
HashSet<E>
Iterator<E> iterator()
Set<E>
<<interface>>
ArrayListIterator<E>
HashSetIterator<E>
LinkedListIterator<E>
Applying Iterators
for (Item next : collection) {
// do something with next
}
- What collection do you want to iterate over? That is your Iterable.
- What type of data comes out of the collection? That identifies the type parameter for the Iterable<???> and Iterator<???>
- Provide an implementation for the iterator() method.
Lecture ticket
Lecture TicketÂ
Thing doll = new Thing("Doll");
Thing puppy = new Thing("Puppy");
Thing marble = new Thing("Marble");
ThingCollection myThings = new ThingCollection();
myThings.add(doll);
myThings.add(puppy);
myThings.add(marble);
for(Thing thing : myThings) {
thing.display();
}
Not compiling:
foreach not applicable
to type 'ThingCollection'
public class Thing {
private String name;
public Thing(String name) {
this.name = name;
}
public void display() {
System.out.println("Behold,
the beautiful " + name);
}
}
Your code
Thing
public class ThingCollection {
private ArrayList<Thing> things = new ArrayList<>();
public void add(Thing thing) {
System.out.println("Ooh --- I have a new " + thing);
things.add(thing);
}
}
ThingCollection
- Part 1: Which class would need to implement
Iterable? - Part 2: What would be passed to Iterable as the type parameter?
- Part 3: Where would the iterator() method be implemented?
- Part 4: What would the iterator() method return?
ThingCollection
Thing
ThingCollection
things.iterator()
Lecture Lab
Things & Remote Controls
Modification while Iterating
for (Item next : collection) {
if ("Chickpeas".equals(next.getName())) {
collection.remove(next);
}
}
This doesn't work!
Iterator<Item> iterator = collection.iterator();
while (iterator.hasNext()) {
Item next = iterator.next();
if ("Chickpeas".equals(next.getName())) {
iterator.remove();
}
}
But this does!
D7: Iterator Pattern
The End - Thank You!
Recommended Additional Reading:
https://refactoring.guru/design-patterns/iterator
CPSC210 - D7: Iterator Pattern
By meghanallen
CPSC210 - D7: Iterator Pattern
- 76