CPSC 210

D7: Iterator Pattern

๐Ÿ„ย ๐Ÿ˜ย ๐Ÿฆ†ย ๐ŸŸย ๐Ÿย ๐Ÿฆ‹ย ๐Ÿธ ๐Ÿฆ–๐ŸŒณย ๐ŸŒธย ๐Ÿ“ย ๐Ÿฅ’ย ๐ŸŽย ๐Ÿฅœ๐ŸŒฟ

Teaching

Please

Evaluation

Learning Goals

  • To apply the Iterator Design Pattern to a given problem

The Iterator Pattern

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
    couldn't
    be that lazy
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
}
  1. What collection do you want to iterate over? That is your Iterable.
  2. What type of data comes out of the collection?ย  That identifies the type parameter for the Iterable<???> and Iterator<???>
  3. 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

Made with Slides.com