Java Collections: Lists and Sets - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Java Collections: Lists and Sets

Description:

Sets example: the set of CDs in a music collection. no duplicates. no inherent order ... might be duplicates. entries are in order (perhaps in the order received) ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 7
Provided by: Steve57
Category:

less

Transcript and Presenter's Notes

Title: Java Collections: Lists and Sets


1
Java Collections Lists and Sets
  • What are Collections?
  • Classes provided (in the java.util package) for
    managing collections of Objects
  • Lists and Sets
  • Sets example the set of CDs in a music
    collection
  • no duplicates
  • no inherent order
  • no ith element
  • you need to iterate over the elements, and add
    and remove elements
  • no fixed size to the collection
  • Lists example phone messages to return
  • might be duplicates
  • entries are in order (perhaps in the order
    received)
  • you can add to the end, or to the beginning, or
    to the middle
  • you can access the ith element
  • no fixed size to the collection

2
The List and Set Interfaces
  • List and Set are interfaces that dictate what a
    class must do to implement the corresponding
    abstract collection

The Set interface (partial) boolean add(Object
o) void clear() boolean contains(Object
o) boolean isEmpty() Iterator iterator() boole
an remove(Object o) int size() Object toArra
y()
The List interface (partial) boolean add(Object
o) void add(int index, Object o) void clear() b
oolean contains(Object o) Object get(int
index) int indexOf(Object o) boolean
isEmpty() Iterator iterator() boolean
remove(Object o) Object set(int index, Object
o) int size() List sublist(int to, int
from) Object toArray()
Question what is the proper interface for our
Course and its Students?
3
The iterator() Method and the Iterator Interface
  • We already know that we often want to give "read
    only" access to an object or collection of
    objects
  • our Student class allows the instance variables
    to be read but not changed
  • likewise the Course class might want an
    application to be able view but not modify the
    collection of Students it contains
  • If we have a List class, we can do this by
    exposing only the methods
  • int size() how many students are in the course
  • Student get(int index) return the ith student
    in the course
  • With a Set class we don't have the get() method.
    Alternatives are
  • give Course a method that will return a copy of
    its set of students, and allow the application
    full access to the copy (but it can't touch the
    original)
  • give Course a "pointer" into the Set like an
    index (that's what the iterator does)

4
Returning an Iterator
Set students
Iterator sti students.iterator()
anIterator
Student 88
Student 12
Student 1
Student 4
5
An Operation on the Iterator
Set students
Iterator sti students.iterator()
anIterator
Student 88
Student 12
Student 1
Student 4
Student st (Student)sti.next()
6
Choosing an Implementation
  • Remember that List and Set are not real objects
    as interfaces they dictate what an implementation
    class must do, but they are not themselves
    implementation class.
  • Although our biggest design decision is which
    collection interface is most appropriate for our
    task, and most of our code programs to the
    interface (only), at some point in the
    application, some code must decide what
    implementation to use.
  • this decision does not affect the semantics of
    the program, but it might affect the efficiency
  • The Java API will tell you what classes implement
    the interface
  • for List we have ArrayList, LinkedList, and
    Vector
  • for Set we have HashSet, LinkedHashSet, and
    TreeSet
  • The choice of the best implementation is
  • an efficiency issue only
  • based on the expected usage pattern for the
    application
  • a topic we will take up a little in this class,
    and a lot in 342
Write a Comment
User Comments (0)
About PowerShow.com