360 likes | 466 Views
The Collections API. CS 242 Fall 2008 Karl R. Wurst. The Collections API. Provides data structures that can be reused All have a standard interface and provide a consistent set of operations (methods) Underlying implementations differ and provide different benefits. The Iterator Pattern.
E N D
The Collections API CS 242 Fall 2008 Karl R. Wurst
The Collections API • Provides data structures that can be reused • All have a standard interface and provide a consistent set of operations (methods) • Underlying implementations differ and provide different benefits
The Iterator Pattern • An Iterator allows stepping though all elements in a collection • You have done this with arrays: for (int i = 0; i < v.length; i++) System.out.println( v[ i ] ); • But this only works for arrays - we want something more general
The Iterator Pattern • A more general Iterator could look something like this: IteratorType itr; for (itr = v.first(); itr.isValid; itr.advance()) System.out.println( itr.getData() ); • We have two objects here • v - our Collection which holds the data • itr - our Iterator which allows us to step through the
Basic Iterator Design • Our first version has two classes and three methods • MyContainer - holds the data • MyContainerIterator iterator() - returns a MyContainerIterator object • MyContainerIterator - has methods to step through the data • Boolean hasNext() - are there more items to iterate over • Object next() - returns the next item in the collection and advances the current position
MyContainer Class • Uses an array internally
The Collection Interface • Represents a group of objects (elements) • May be ordered or unordered, depending on implementation • May allow duplicates, depending on implementation • All provide a minimum set of operations
Interface issues • To implement an interface, you are required to provide all methods specified in the interface • Some methods may not make sense for our implementation - say remove() • Implement the method to throw UnsupportedOperationException • And be sure to document it so anyone using your class is not surprised!
Using the Collection and Iterator • The second version works because Iterator implements Iterable
The List Interface • A list is a collection of items with positions
The ListIterator Interface • Works forwards and backwards • The method to get the Iterator takes a starting position • 0 - start at the beginning and go forward • size of the list - start at the end and go backward
LinkedList • Objects are stored in nodes containing the object and a reference to the next node
LinkedList • Additional operations: • void addLast(AnyType element) • void addFirst(AnyType element) • AnyType getFirst() • AnyType element() • AnyType getLast() • AnyType removeFirst() • AnyType remove() • AnyType removeLast()
Stacks and Queues • Do not allow access to all elements directly • Stack - Last In-First Out (LIFO) • Queue - First In-First Out (FIFO)
Stack • LIFO
Stack • Java provides Stack class, but it is based on Vector, so it can be slow • LinkedList can be used to implement Stack • add() for push() • element() for top() • remove() for pop()
Queue • FIFO
Queue • Java does not provide Queue class • LinkedList can be used to implement Queue • addLast() for enqueue() • element() for getFront() • remove() for dequeue()
Set • Sets contain no duplicates • SortedSet keeps items in sorted order • Elements are either Comparable (Natural Order) • Or, a Comparator is provided • Iteratorwill provide elements in sorted order • AnyType first() - smallest element • AnyType last() - largest element • TreeSet implements SortedSet • O(log N)
HashSet • Does not keep elements in sorted order • On average, performs in constant time • Will work with no additional information, but works better if you provide int hashCode() • int hashCode() should always provide the same value if equals() is true • int hashCode() should try to provide different values for items that are not equal
Maps • Maps store pairs of keys and values • Keys must be unique • Values do not have to be unique • HashMap does not maintain sorted keys • TreeMap maintains sorted keys • Provides no Iterator, instead • Returns a Set of keys - keySet() • Returns a Collection of values - values() • Returns a Set of Entry - entrySet() • Entry is a class containing Key/Value pairs
PriorityQueue • Elements are added to the queue, but minimum value is removed first • Used often in operating systems • Java provides a PriorityQueue class