CS226 -- Day 13 -- Spring 2013 Finish Iterable Bag (Bagi) examples: BagiArray.java, BagiList.java Java Collections Framework - java.util package -------------------- Tutorial: http://docs.oracle.com/javase/tutorial/index.html Collection extends Iterable - group of items all of type T with basic add, remove, contains - analogous to our Bagi List extends Collection - adds positional operations for indexing into collection QUEUES, SETS, MAPS - preview of things to come... QUEUE - Queue extends Collection - usually first-in, first-out (FIFO), but could be priority queue - adds to tail, removes and peeks at head - additional operations: E peek(); // what is next element to be removed E remove(); // remove next element E element(); // peek variation boolean offer(E e); // add variation E poll(); // remove variation SET - Set extends Collection - adds unique property - SortedSet extends Set - adds ordering property - insert, remove, contains - TreeSet is implementation - operations are worst case logarithmic (Red-Black BBST) MAP - key,value pairs - each pair is an Entry (nested class) - unique keys!! - SortedMap - keeps keys in order Collections Details ----------------------------------------------- java.util.Collection extends Iterable (like our Bagi) most common methods: - int size() - void clear() - boolean isEmpty() - boolean contains(T o) - boolean add(T) - boolean remove(T o) // removes T if there - Iterator iterator() // bc implements Iterable General Positional List ADT (Sequence) - all elements occupy a numbered position (not the same as sorted) - positions of N element list go from 0 to N-1 - common operations: String analogy - size() length() - print() toString() - clear() new String() - where find(item) indexOf(char) - item get(where) charAt(index) - insert(item, where) - remove(item) List interface extends Collection - get(where) - set(where, item) - add(where, item) - insert into position where, maintain relative order - remove(where) - remove from position where, maintain relative order Positional List Implementations ------------------------------------ - array implementation (ArrayList in Java) - singly linked list implementation - doubly linked list implementation (LinkedList in Java) - how long will each operation take & how to implement? - small group worksheets... - see positionalLists.xlsx for results - array implementation: ArrayList in Collections API - implementation uses extendable array (doubling) - should also shrink if gets too small - for extending an array - AMORTIZED analysis: - charge 2 cyber dollars per add op - each one pays to add (1 op) and pays to copy one previously added when the array size is doubled array size charged add costs copy costs .4 4(new)*2 4 0 .4.4 4(new)*2 4 (new) 4 (old) ...8...8 8(new)*2 8 (new) 8 (old) ......16......16 16(new)*2 16 (new) 16 (old) - an amortized cost is the accumulated cost / # operations - therefore we have amortized cost for 1 add is O(1) - admittedly, one add will be very slow (when copying) compared to the others, but it is essentially paying for the next group - doubly linked list - API LinkedList - doubly linked list implementation - special operations for front and back of list - any operations at either end are constant - can have bi-directional iterator - use of iterator much more efficient than loop through indices because must iterate to each position - java.util.ListIterator interface - gives forwards and backwards motion (for java.util.LinkedList) - current element depends on most recent next or prev operation