What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.

Slides:



Advertisements
Similar presentations
Java Programming: Advanced Topics 1 Collections and Utilities.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
Multiple Choice Solutions True/False a c b e d   T F.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Problem Of The Day  Decipher the following phrase: STANDS 0 _
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
1 Unit 10 - Data Structures Objectives 1. Describe the usage of data structures. 2. Develop simple data structures. 3. Apply Java collections. 4. Apply.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
(c) University of Washington14-1 CSC 143 Java Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Grouping objects Arrays, Collections and Iterators 1.0.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Chapter 18 Java Collections Framework
LinkedList Many slides from Horstmann modified by Dr V.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
Iterator Pattern. Traversing two different collections  When bringing two previously developed objects together, it can be difficult to change an implementation.
Iteration Abstraction SWE Software Construction Fall 2009.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS2005 Week 7 Lectures Set Abstract Data Type.
Building Java Programs
University of Central Florida COP 3330 Object Oriented Programming
Building Java Programs
CSE 143 Lecture 27: Advanced List Implementation
Iterator.
Lecture 26: Advanced List Implementation
What is Singleton Category: Creational pattern
Iteration Abstraction
Lecture 7: Linked List Basics reading: 16.2
Software Design Lecture : 39.
Java Generics & Iterators
Presentation transcript:

What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion with which the collection is traversed

How to Use Iterator Implementation Details –Have a class (Class A) with which iteration makes sense –Create a class (Class B) that implements the java.util.Iterator interface –Class B should manage the iteration state over Class A for a single client. Usage Details –Call constructing iterator() method on collection object. –Use Iterator object returned to iterate elements (next(), hasNext())

How to Use Iterator Example Iterator implementation (note: similar to actual Java implementation) // Note: Some features are not implemented… public class VectorIterator implements java.util.Iterator { private Vector collection; private int cursor=0; public VectorIterator(Vector toIterate) { collection = toIterate; } public boolean hasNext() { return (cursor < collection.size()); } public void next() { cursor++; Object next = collection.get(cursor); }

How to Use Iterator Traditional Looping Vector aVector = new Vector(); // load vector with elements // Implementation omitted for(int i=0; i<aVector.size();i++) { Object anElem = aVector.get(i); } Iterator Looping Vector aVector = new Vector(); // load vector with elements // Implementation omitted Iterator elements = aVector.iterator(); while(elements.hasNext()) { Object anElem = elements.next(); }

Class Diagram From (Stephen Stelting, Olav Maassen, Applied Java™ Patterns)

Why Iterator Hides details of Collection Traversal Avoids the ugly, and sometimes bug-prone index/cursor management (int i=…) The iterator pattern can be used to disperse the cost of expensive object construction

In the Real World Java has built in Iterator Support for the Collections Library (java.util.Iterator) Vector, ArrayList, LinkedList, HashSet, TreeSet, etc (Collection.iterator() method). Iterator may be traversing an Array, a LinkedList, or even a binary tree, but the code using the library knows no difference. Developers can reuse the Iterator interface for their own libraries/implementations

In the Real World Iterator can be used to defer expensive reads such as database calls and file parsing until it is actually necessary. In combination with the factory pattern, collection type choices can be completely abstracted from the program, and into configuration files.

Advanced Java Examples public class CollectionsExample { public static void main(String[] args) { Collection collectionA = null; collectionA = createCollection(args); iterateCollection(collectionA); } public static Collection createCollection(String[] args) { Collection collection = null; if(args[0].equals(“Vector”)) { // returns a index based list walking iterator collection = new Vector(); } else if(args[0].equals(“TreeSet”)) { // returns a tree walking iterator. collection = new TreeSet(); } return collection; } // Loop code stays the same regardless of the implementation public static void iterateCollection(Collection collection) { Iterator elements = collection.iterator(); while(elements.hasNext()) { Object anObj = elements.next(); }