# Computer Science 209 Software Development Iterators.

## Presentation on theme: "Computer Science 209 Software Development Iterators."— Presentation transcript:

Computer Science 209 Software Development Iterators

Implementing Equals public boolean equals(Object other){ if (this == other) return true; if (! (other instanceof LinkedStack)) return false; LinkedStack otherStack = (LinkedStack )other; if (this.size() != otherStack.size()) return false; return this.containsAll(otherStack) && otherStack.containsAll(this); } Can result in quadratic running time What if the two stacks contain the same elements, but they are not in the same order?

Implementing Equals public boolean equals(Object other){ if (this == other) return true; if (! (other instanceof LinkedStack)) return false; LinkedStack otherStack = (LinkedStack )other; if (this.size() != otherStack.size()) return false; for (int i = 0; i < this.size(); i++) if (! this.list.get(i).equals(otherStack.list.get(i))) return false; return true; } get runs in constant time for ArrayList but in linear time for LinkedStack Results in quadratic running time for LinkedStack.equals

Implementing Equals public boolean equals(Object other){ if (this == other) return true; if (! (other instanceof LinkedStack)) return false; LinkedStack otherStack = (LinkedStack )other; if (this.size() != otherStack.size()) return false; Iterator otherIter = otherStack.iterator(); for (E thisElement : this) if (! thisElement.equals(otherIter.next())) return false; return true; } next runs in constant time for any collection’s iterator Results in linear running time for any equals

The Iterator Interface public interface Iterator { public boolean hasNext() public E next() public void remove() } remove deletes the object most recently accessed with next remove must be included in the implementing class, but need not be supported (can throw an UnsupportedOperationException )

Using an Iterator // add a bunch of objects to col Iterator iter = col.iterator(); Every collection class that supports an iterator must provide an iterator method. This method returns an instance of a class that implements the Iterator interface anIteratoraCollection A sequence of elements

// add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); System.out.println(obj); } Using an Iterator DDD collection iterator

// add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); System.out.println(obj); } Using an Iterator DDD collection iterator

// add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); System.out.println(obj); } Using an Iterator DDD collection iterator

// add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); System.out.println(obj); } Using an Iterator DDD collection iterator

// add a bunch of objects to col for (SomeType obj : col) System.out.println(obj); Iterable and the for -each Loop DDD collection iterator If col implements the Iterable interface, the client can use a for -each loop instead

Use in AbstractCollection abstract public class AbstractCollection implements Collection { public void clear(){ Iterator iter = this.iterator(); while (iter.hasNext()){ iter.next(); iter.remove(); } public boolean remove(Object o){ Iterator iter = this.iterator(); while (iter.hasNext()) if (iter.next().equals(o)){ iter.remove(); return true; } return false; }

public boolean hasNext() public E next() Preconditions on Methods hasNext has no preconditions next has two preconditions: hasNext returns true the underlying collection has not been modified by one of that collection’s mutators during the lifetime of that iterator

// Add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ SomeType obj = iter.next(); } SomeType obj = iter.next(); // This should cause an exception Error: Run Out of Elements

// Add a bunch of objects to col Iterator iter = col.iterator(); while (iter.hasNext()){ col.removeLast(); SomeType obj = iter.next(); // This should cause an exception } Error: Inconsistent Data Using mutators in conjunction with iterators is a bad practice

An Iterator Implementation The iterator method is in the Iterable interface public interface TrueStack extends Collection { public E pop(); public void push(E newElement); public E peek(); } > Collection > Iterable > TrueStack

public class ArrayStack extends AbstractCollection implements TrueStack { private List list; // Code for constructors, push, pop, peek, size, and add public Iterator iterator(){ return list.iterator(); } An Iterator Implementation Problem: a list’s iterator supports the remove method

Another Design Strategy By using the list’s iterator, we expose it to the client (Law of Demeter?) Let’s use it, but wrap our own iterator object around it That allows us to control what’s supported and what’s not

public class ArrayStack extends AbstractCollection implements TrueStack { private List list; // Code for push, pop, peek, size, and add // Code for the iterator method // Code for the class that implements the Iterator // interface } An Iterator Implementation Define the iterator class as a private inner class.

public Iterator iterator(){ return new StackIterator (this.iterator()); } private class StackIterator implements Iterator { public boolean hasNext(){ return false; } public E next(){ return null; } public void remove(){ } The Implementing Class Nested within ArrayStack

public Iterator iterator(){ return new StackIterator (list.iterator()); } private class StackIterator implements Iterator { private Iterator iter; private StackIterator(Iterator iter){ this.iter = iter; } // Other methods } The Implementing Class Nested within ArrayStack

private class StackIterator implements Iterator { private Iterator iter; public boolean hasNext(){ return iter.hasNext(); } public E next(){ return iter.next(); } public void remove(){ throw new UnsupportedOperationException( "remove not supported by Stack"); } Other Methods