COMP 103 Iterators and Iterable RECAPTODAY 2 RECAP
COMP 103 Iterators and Iterable
RECAP-TODAY 2 RECAP Maps and Queues TODAY Queue Methods Iterator and Iterable
3 Iterators List <Task> my. Tasks = new Array. List <Task> (); my. Tasks Iterator <Task> iter = my. Tasks. iterator(); iter Conforms to the interface Iterator Conforms to the interface List so… has methods • add() • remove() • contains(), etc… so… has methods • has. Next() • next() • remove()
Why Iterators? 4 My. Program cannot get inside a Collection object to do "for each. . . ". The Collection object has to go through its members, itself. Jack Jane Jacob Julia Has to be "Iterable" (i. e. can construct itself an Iterator object) A N P D F C H E K J O M L W K Justin Joleen John My. Program List<Creature> mob; : Iterator<Creature> has. Next next for (Creature c : mob) { } List <Creature> Data
5 Iterator Interface Operations on Iterators: has. Next() next() remove() : returns true iff there is another value to get : returns the next value : rarely used Standard pattern of use: Iterator <type > itr = construct iterator while (itr. has. Next() ){ type var = itr. next(); … var … } For each loop is convenient shorthand for this use: for (type var : Iterable<type> ){ … var … e. g. all Collection, since they extend Iterable
6 Making Collections Iterable Iterator public Iterator<T> iterator() • has. Next() • next() • remove() extends Collection • size() • add() etc implements Array. List. Iterat Array. List or extends List • All Collection methods + some own : implements all public Iterator<T> iterator() Iterator methods : : ? Array. List. Iterat or Array. List implements allsits Listinside as a methods private inner class + the ONE method of Iterable
7 Example public class Array. Bag <E> implements “some Iterable type” <E>{ : : e. g. implements : Bag<String> public Iterator<E> iterator(){ OR extends some return new Array. Bag. Iterator<E>(this); abstract class – } later! : : private class Array. Bag. Iterator <E> implements Iterator<E>{ //constructor //define has. Next() method //define next() method //define remove method }//closing private inner class }//closing outer public class
8 [Aside: multiple classes in a file ] Only one public class per file � Name of public class must be same as the file name. Additional classes in the same file, after public class � Must not be public, or private � Can be called by the public class, or each other � Cannot be called from the command line (no “main” method) � This style wraps a whole program into a single file, but makes it less reusable. Can have additional classes inside other classes � May be private – accessible only by enclosing class � May be public – generally accessible � Appropriate when inner class is strongly connected to enclosing class (as with an iterator for example) Can also have anonymous inner classes inside
Example 9 We have to be able to iterate over a Collection. How? Collection should implement the Iterable interface! this iterator should implement the Iterator interface then any implementation of Collection will have to provide an "iterator()" method, which returns an iterator object (!) specific to that implementation. it must have two methods: has. Next(), next(), and remove() the Collections interfaces (List, Queue. . . ) are Iterable, so the Collections classes (Array. List, Linked. List. . . ) implement iterator(), which returns an Iterator object. QUICK TIP: Map does not implement Iterable! You can iterate over Map via the three “collection views”: SET of keys, COLLECTION of values, SET of Map. Entry (key-value pairs)
10 Creating Iterators are not just for Collection objects: Anything that can generate a sequence of values Scanner Pseudo Random Number generator : public class Rand. Num. Iter implements Iterator<Integer>{ private int num = 1, public boolean has. Next(){ remove() is an return true; // there is always another one! optional method: must } be defined, but public Integer next(){ doesn’t need to do num = (num * 92863) % 104729 + 1; anything! return num; } public void remove(){throw new Unsupported. Operation. Exception(); } } Iterator<Integer> rand. Nums = new Rand. Num. Iter(); for (int i = 1; i<1000; i++) UI. print(rand. Nums. next()+ "n");
Creating an Iterable (independent of collections) 11 Iterables are not just for Collection types An Iterable<E> is an object that provides an Iterator<E>: eg: An Arith. Sequence representing an infinite arithmetic sequence of numbers, with a starting number and a step size, eg 6, 9, 12, 15, 18, …. public class Arith. Sequence implements Iterable <Integer> { private int start; private int step; public Arith. Sequence(int start, int step) { this. start = start; this. step = step; } "this" is needed, if using same name : : public Iterator <Integer> iterator() { return new Arith. Sequence. Iterator(this); } constructor, just sets the first value and the step size make an iterator
so this class is only accessible from inside Arith. Sequence! An Iterator, for an Iterable 12 : //continued private class Arith. Sequence. Iterator implements Iterator <Integer> { private int next. Num; private Arith. Sequence source; public Arith. Sequence. Iterator(Arith. Sequence seq) { source = seq; next. Num = seq. start; } public boolean has. Next() { return true; // there is always another one } public Integer next() { int ans = next. Num; next. Num += source. step; return ans; } public void remove() {throw new Unsupported. Operation. Exception(); } } // end of Arith. Sequence. Iterator class } // end of Arith. Sequence class
13 Using the Iterable Can use the iterable object in the foreach loop: for (int n : new Arith. Sequence(15, 8)){ System. out. printf(“next number is %d n”, n); } Can use the iterator of the iterable object directly. Arith. Sequence seq = new Arith. Sequence(15, 8); Iterator<Integer> iter = seq. iterator(); Constructs a multi page table of the process. First. Page(iter); for (int p=2; p<max. Pages; p++) sequence. The process…Page methods continue the sequence from the previous page process. Next. Page(p, iter); Notice you can pass iterator to different methods to deal with: ⇒ can “spread out” a for loop
Working with Collections 14 Done: Declaring and Creating collections ü Using collections: adding, removing, getting, setting, putting, …. ü Iterating through collections ü [ Iterators, Iterable, and the "for each" loop ] What next? Comparable and Comparator for Sorting Collections Implementing Collection classes
- Slides: 14