The collections api
This presentation is the property of its rightful owner.
Sponsored Links
1 / 36

The Collections API PowerPoint PPT Presentation


  • 44 Views
  • Uploaded on
  • Presentation posted in: General

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.

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


Using the container and iterator

Using the Container and Iterator


Mycontainer class

MyContainer Class

  • Uses an array internally


Mycontaineriterator class

MyContainerIterator Class


Inheritance based iterator

Inheritance-Based Iterator


Inheritance based iterator1

Inheritance-Based Iterator


Using the inheritance based iterator

Using the Inheritance-Based Iterator


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


The iterator interface

The Iterator Interface


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 list interface1

The List Interface


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


Linkedlist

LinkedList

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


Linkedlist1

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

Stacks and Queues

  • Do not allow access to all elements directly

  • Stack - Last In-First Out (LIFO)

  • Queue - First In-First Out (FIFO)


Stack

Stack

  • LIFO


Stack1

Stack


Stack2

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

Queue

  • FIFO


Queue1

Queue


Queue2

Queue

  • Java does not provide Queue class

  • LinkedList can be used to implement Queue

    • addLast() for enqueue()

    • element() for getFront()

    • remove() for dequeue()


The collections api

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

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


The collections api

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

PriorityQueue

  • Elements are added to the queue, but minimum value is removed first

  • Used often in operating systems

  • Java provides a PriorityQueue class


Summary

Summary


  • Login