Docsity

Java Sequences and Iterators: Lists and Iterators in Java, Slides of Data Representation and Algorithm Design

Toggle sidebar
bg1
2.7 Lists and Iterators
2
Sequences and Urns
Sequence. Ordered collection of items.
Key operations. Insert an item, iterate over the items.
Design challenge. Support iteration by client, without revealing the
internal representation of the collection.
was the best ofIt times
next
Robert Sedgewick and Kevin Wayne • Copyright © 2006 • http://www.Princeton.EDU/~cos226
Iteration in Java
4
Iterator Interface
API for java.util.Iterator.
!hasNext() Are there more items in the list?
!next() Return the next item in the list.
!remove() Delete the last item returned by next().
public interface Iterator<Item> {
boolean hasNext();
Item next();
void remove(); // optional
}
was the best ofIt times
next
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Java Sequences and Iterators: Lists and Iterators in Java and more Slides Data Representation and Algorithm Design in PDF only on Docsity!

2.7 Lists and Iterators

2

Sequences and Urns

Sequence. Ordered collection of items.

Key operations. Insert an item, iterate over the items.

Design challenge. Support iteration by client, without revealing the

internal representation of the collection.

It was the best of times next Robert Sedgewick and Kevin Wayne • Copyright © 2006 • http://www.Princeton.EDU/~cos 226

Iteration in Java

4

Iterator Interface

API for java.util.Iterator.

! hasNext() Are there more items in the list?

! next() Return the next item in the list.

! remove() Delete the last item returned by next().

public interface Iterator { boolean hasNext(); Item next(); void remove(); // optional } It was the best of times next

5 public static void main(String[] args) { Sequence list = new Sequence(); list.add("This"); list.add("is"); list.add("a"); list.add("test."); Iterator i = list.iterator(); while (i.hasNext()) { String s = i.next(); System.out.println(s); } }

Iterator Client

API for java.util.Iterator.

! hasNext() Are there more items in the list?

! next() Return the next item in the list.

! remove() Delete the last item returned by next().

6

Iterable Interface

API for java.lang.Iterable.

! iterator() Return an iterator.

Ex. Sequence, java.util.ArrayList, HashSet.

public interface Iterable { Iterator iterator(); } 7

Enhanced For Loop

Enhanced for loop. Syntactic sugar for iterating over a collection.

Remark. Can also use enhanced for loop with arrays.

public static void main(String[] args) { Sequence list = new Sequence(); list.add("This"); list.add("is"); list.add("a"); list.add("test."); for (String s : list) System.out.println(s); } implements Iterable! can iterate using enhanced for loop

Sequence ADT: Two Implementations

Applications

14

Load Balancing

Load balancing. N users want to choose among N identical file shares.

The goal is to balance users across file shares. Assume it's too hard to

coordinate (or query) all resources to see how empty they are.

Random assignment. Assign each user to a resource at random.

% java LoadBalance 10 0 : 1 : 2 : user 7 3 : user 1 user 2 user 8 4 : user 0 user 9 5 : 6 : user 3 user 6 7 : 8 : user 4 9 : user (^5) max load = 3 15

Server.java

public class Server { private Sequence list = new Sequence(); private int load; public void add(String user) { list.add(user); load++; } public String toString() { String s = ""; for (String user : list) s += user + " "; return s; } } 16

Load Balancing

public class LoadBalance { public static void main(String[] args) { int N = Integer.parseInt(args[ 0 ]); Server[] servers = new Server[N]; for (int i = 0 ; i < N; i++) servers[i] = new Server(); // assign N users to N servers at random for (int j = 0 ; j < N; j++) { String user = "user" + j; int i = (int) (Math.random() * N); servers[i].add(user); } // print results for (int i = 0 ; i < N; i++) System.out.println(i + ": " + servers[i]); } }

17

Load Balancing

Load balancing. N users want to choose among N identical file shares.

The goal is to balance users across file shares. Assume it's too hard to

coordinate (or query) all resources to see how empty they are.

Coordinated assignment. Assign user i to server i.

Result. Max load = 1.

Random assignment. Assign each user to a resource at random.

Theory. Max load " log N / log log N.

Best of two. For each user, choose two resources at random and assign

user to least busy one.

Theory. Max load " log log N.

18

Java List Libraries: ArrayList and LinkedList

API for java.util.ArrayList.

! add() Add item to end of list.

! iterator() Return an iterator to the list.

! size(), remove(), set(), clear(), indexOf(), toArray(), ….

import java.util.ArrayList; public class Test { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("This"); list.add("is"); list.add("a"); list.add("test."); for (String s : list) System.out.println(s); } }

Tree Iterators

20

Binary Tree Iterator

Binary tree. Create an iterator for a binary tree.

(and avoid using extra space)

public class BinaryTree { private Node root; private class Node { Item item; Node l, r; } public Iterator iterator() { return new Preorder(); } } goal: implement this B E M P T H W V Y