1 / 36

The Collections API

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.

hollye
Download Presentation

The Collections API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Collections API CS 242 Fall 2008 Karl R. Wurst

  2. 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

  3. 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

  4. 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

  5. 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

  6. Using the Container and Iterator

  7. MyContainer Class • Uses an array internally

  8. MyContainerIterator Class

  9. Inheritance-Based Iterator

  10. Inheritance-Based Iterator

  11. Using the Inheritance-Based Iterator

  12. 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

  13. The Iterator Interface

  14. 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!

  15. Using the Collection and Iterator • The second version works because Iterator implements Iterable

  16. The List Interface • A list is a collection of items with positions

  17. The List Interface

  18. 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

  19. LinkedList • Objects are stored in nodes containing the object and a reference to the next node

  20. LinkedList • Additional operations: • void addLast(AnyType element) • void addFirst(AnyType element) • AnyType getFirst() • AnyType element() • AnyType getLast() • AnyType removeFirst() • AnyType remove() • AnyType removeLast()

  21. Stacks and Queues • Do not allow access to all elements directly • Stack - Last In-First Out (LIFO) • Queue - First In-First Out (FIFO)

  22. Stack • LIFO

  23. Stack

  24. 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()

  25. Queue • FIFO

  26. Queue

  27. Queue • Java does not provide Queue class • LinkedList can be used to implement Queue • addLast() for enqueue() • element() for getFront() • remove() for dequeue()

  28. 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)

  29. 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

  30. 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

  31. PriorityQueue • Elements are added to the queue, but minimum value is removed first • Used often in operating systems • Java provides a PriorityQueue class

  32. Summary

More Related