Interator and Iterable

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Computer Science 209 Software Development Iterators.
©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.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
1 Collections Working with More than One Number or Data Type or Object.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
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.
CS 307 Fundamentals of Computer ScienceIterators 1 Topic 14 Iterators "First things first, but not necessarily in that order " -Dr. Who.
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
Chapter 9: The Iterator Pattern
LinkedList Many slides from Horstmann modified by Dr V.
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.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
List Interface and Linked List Mrs. Furman March 25, 2010.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
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.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Java Generics And collections
Slides by Donald W. Smith
EKT472: Object Oriented Programming
Iterators.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Chapter 4: A Basic Collection Class
EECE 310: Software Engineering
Interfaces.
Software Development Iterators
ADT’s, Collections/Generics and Iterators
Iterator Design Pattern
Adapter, Fascade, and More
Java Generics.
Introduction to Data Structures
Iteration Abstraction
Generics A Brief Review 16-Nov-18.
Streams.
"First things first, but not necessarily in that order " -Dr. Who
08/15/09 Design Patterns James Brucker.
Collections A First Glimpse.
Generics 27-Nov-18.
(Java Collections Framework) AbstractSequentialList
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Arrays versus ArrayList
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Menu item at a restaurant
"First things first, but not necessarily in that order." -Dr. Who
Chapter 5: Enhancing Classes
Iteration Abstraction
COMPUTER 2430 Object Oriented Programming and Data Structures I
Collections James Brucker.
slides created by Ethan Apter
"First things first, but not necessarily in that order " -Dr. Who
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
"First things first, but not necessarily in that order " -Dr. Who
Collections A First Glimpse 16-Apr-19.
slides created by Alyssa Harding
slides created by Alyssa Harding
Creating and Modifying Text part 3
Review: libraries and packages
Generics 2-May-19.
Software Design Lecture : 39.
Objects with ArrayLists as Attributes
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

Interator and Iterable Two commonly used interfaces in Java. And: for-each loop.

<<interface>> Iterator Problem: how can we access all members of a collection without knowing the structure of the collection? Solution: each collection (List, Set, Stack, ...) provides an Iterator we can use. <<interface>> Iterator hasNext( ): boolean next( ): T (Object) remove(): void

How to Use Iterator List<String> list = new ArrayList<String>( ); list.add("apple"); list.add( ... ); // add stuff Iterator<String> iter = list.iterator(); while ( iter.hasNext() ) { String s = iter.next( ); // do something with s } create a new Iterator for the collection.

Iterator Reduces Dependency Suppose we have a Purse that contains Coins and a method getContents to show what is in the purse: // Suppose a purse has a collection of coins List<Coin> coins = purse.getContents(); for(int k=0; k<coins.size(); k++) { Coin c = coins.get(k); //TODO process this coin } Now the Purse must always create a List for us, even if the coins are stored is some other kind of collection, or a database.

Iterator Reduces Dependency (2) If getContents instead just returns an Iterator, then: // Suppose a purse has a collection of coins Iterator<Coin> coins = purse.getContents(); while( coins.hasNext() ) { Coin c = coins.next(); //TODO process this coin } The purse is free to internally use any collection it wants, and does not need to create a List for us.

<<interface>> Iterable Problem: how can we get an Iterator? Forces: (1) the collection should create the iterator itself since only the collection knows its own elements. (2) every collection should provide same interface for getting an Iterator (for polymorphism). Solution: define an interface for creating iterators. Make each collection implement this interface. <<interface>> Iterable iterator( ): Iterator<T>

How to Use Iterable List<Student> list = new ArrayList<String>( ); list.add( ... ); Iterator<String> iter = list.iterator(); iterator() creates a new Iterator each time.

Iterable is a Factory Method You can eliminate direct dependency between classes by creating an interface for required behavior. the factory... the product... abstract: the factory method concrete:

Factory Method The Pattern Factory Interface Iterable Factory Method iterator( ) Product Iterator Concrete Factory any collection

for-each loop List<String> list = new ArrayList<String>( ); list.add( . . . ); // add things to list // print the list for( String s: list ) { System.out.println(s); } "for each String s in list" { . . . }

for-each compared to while For-each loop. stuff is any Collection or an array. for( Object x: stuff) { System.out.println( x ); } While loop does same thing: Iterator iterator = stuff.iterator( ); while( iterator.hasNext() ) { Object x = iterator.next( ); System.out.println( x ); }

for-each in detail "For each Datatype x in ... do { . . . }" for( Datatype x: _collection_ ) { System.out.println(x); } Datatype of the elements in _collection_ _collection_ can be: 1) array 2) any Iterable object

for-each with array Indexed for loop. array[ ] is an array of double double [] array = . . .; for(int k=0; k<array.length; k++) { System.out.println(array[k]); } for-each loop to do the same thing: for( double x: array ) { System.out.println( x ); }

Error: modifying a collection while using iterator Iterator throws an exception if the underlying collection is modified while using the iterator. List<String> words = /* a list of strings */; Iterator iter = words.iterator( ); System.out.println(iter.next()); // OK words.add("elephant"); System.out.println(iter.next()); // error // exception thrown "for-each" also throws exception if you modify collection while inside loop. (what about for-each with array?)