the collections api n.
Skip this Video
Download Presentation
The Collections API

Loading in 2 Seconds...

play fullscreen
1 / 36

The Collections API - PowerPoint PPT Presentation

  • Uploaded on

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'The Collections API' - hollye

Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
the collections api

The Collections API

CS 242 Fall 2008

Karl R. Wurst

the collections api1
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
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 pattern1
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
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
MyContainer Class
  • Uses an array internally
the collection interface
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
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
Using the Collection and Iterator
  • The second version works because Iterator implements Iterable
the list interface
The List Interface
  • A list is a collection of items with positions
the listiterator interface
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
  • Objects are stored in nodes containing the object and a reference to the next node
  • 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
Stacks and Queues
  • Do not allow access to all elements directly
  • Stack - Last In-First Out (LIFO)
  • Queue - First In-First Out (FIFO)
  • LIFO
  • 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()
  • FIFO
  • Java does not provide Queue class
  • LinkedList can be used to implement Queue
    • addLast() for enqueue()
    • element() for getFront()
    • remove() for dequeue()
  • 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)
  • 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 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
  • Elements are added to the queue, but minimum value is removed first
  • Used often in operating systems
  • Java provides a PriorityQueue class