Software Development Iterators

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Computer Science 112 Fundamentals of Programming II List Iterators.
Concrete collections in Java library
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Java Review Interface, Casting, Generics, Iterator.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Working With Collections in the AP ™ Java Subset 2003 ACTE Convention © 2003 by Kenneth A. Lambert.
Computer Science 209 Software Development Iterators.
COMP 103 Linked Stack and Linked Queue.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
1 Topic 8 Iterators "First things first, but not necessarily in that order " -Dr. Who.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Iterators CS 367 – Introduction to Data Structures.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
1 Iterators "First things first, but not necessarily in that order " -Dr. Who CS Computer Science II.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Computer Science 209 Software Development Inheritance and Composition.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Computer Science 209 Software Development Refactoring.
Iterator Pattern. Traversing two different collections  When bringing two previously developed objects together, it can be difficult to change an implementation.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Iterators.
Objectives After studying this chapter you should understand the following: the role of iterators in container classes; various implementations of the.
Chapter 4: A Basic Collection Class
Some Collections: BAGS, SETS, and STACKS
Software Development Java Classes and Methods
ADT’s, Collections/Generics and Iterators
Week 3 - Friday CS221.
Software Development Inheritance and Composition
THURSDAY, OCTOBER 17 IN LAB
Introduction to Collections
ArraySet Methods and ArrayIterator
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
"First things first, but not necessarily in that order." -Dr. Who
Interator and Iterable
"First things first, but not necessarily in that order " -Dr. Who
Chapter 4 Queues.
List Implementation via Arrays
Computer Science 209 The Adapter Pattern.
"First things first, but not necessarily in that order " -Dr. Who
CSC 143 Java Linked Lists.
slides created by Alyssa Harding
Part of the Collections Framework
Java Generics & Iterators
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Software Development Iterators Computer Science 209 Software Development Iterators

Implementing equals for Sets public boolean equals(Object other){ if (this == other) return true; if (! (other instanceof Set)) return false; List<E> otherSet = (Set)other; if (this.size() != otherSet.size()) return false; return this.containsAll(otherSet); } Works correctly, because order of items is not implied Worst-case quadratic running time

Implementing equals for Lists public boolean equals(Object other){ if (this == other) return true; if (! (other instanceof List)) return false; List<E> otherList = (List)other; if (this.size() != otherList.size()) return false; for (int i = 0; i < this.size(); i++) if (! this.get(i).equals(otherList.get(i))) return false; return true; } Works correctly by accounting for the order of the items get runs in constant time for ArrayList but in linear time for LinkedList Worst-case quadratic running time for LinkedList.equals

Implementing equals with iterator public boolean equals(Object other){ if (this == other) return true; if (! (other instanceof List)) return false; List<E> otherList = (List)other; if (this.size() != otherList.size()) return false; Iterator<E> otherIter = otherList.iterator(); for (E thisElement : this) if (! thisElement.equals(otherIter.next())) return false; return true; } Works correctly by accounting for the order of the items next runs in constant time for any collection’s iterator Worst-case linear running time for equals with lists

The Iterator Interface public interface Iterator<E>{ public boolean hasNext() public E next() public void remove() } remove deletes the item 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<SomeType> 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 A sequence of elements anIterator aCollection

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

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

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

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

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

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

Preconditions on Methods public boolean hasNext() public E next() 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

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

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

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

A Naïve Iterator Implementation public class ArrayStack<E> extends AbstractCollection<E> implements TrueStack<E>{ private List<E> list; // Code for constructors, push, pop, peek, and size public Iterator<E> iterator(){ return list.iterator(); } Problem: a list’s iterator supports the remove method This would violate the spirit of a stack

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

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

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

The Implementing Class public Iterator<E> iterator(){ return new StackIterator<E>(list.iterator()); } private class StackIterator<E> implements Iterator<E>{ private Iterator<E> iter; private StackIterator(Iterator<E> iter){ this.iter = iter; // Other methods Nested within ArrayStack The stack iterator encapsulates the list iterator, hiding it from the client (proxy pattern)

Other Methods private class StackIterator<E> implements Iterator<E>{ private Iterator<E> iter; public boolean hasNext(){ return iter.hasNext(); } public E next(){ return iter.next(); public void remove(){ throw new UnsupportedOperationException( "remove not supported by stacks"); Must add javadoc with preconditions and code to check them

For Wednesday GUIs with Swing and AWT