Collections - PowerPoint PPT Presentation

About This Presentation
Title:

Collections

Description:

Vectors have been redefined to implement Collection ... Arrays do not implement the Collection interfaces. 3. Types of Collection ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 14
Provided by: davidma75
Category:

less

Transcript and Presenter's Notes

Title: Collections


1
Collections
  • A First Glimpse

2
Collections
  • A collection is a structured group of objects
  • An array is a kind of collection
  • A Vector is a kind of collection
  • A linked list is a kind of collection
  • Java 1.2 introduced the Collections Framework and
    provided many great implementations
  • Vectors have been redefined to implement
    Collection
  • Trees, linked lists, stacks, hash tables, and
    other classes are implementations of Collection
  • Arrays do not implement the Collection interfaces

3
Types of Collection
  • Java supplies several types of Collection
  • Set cannot contain duplicate elements, order is
    not important
  • SortedSet like a Set, but order is important
  • List may contain duplicate elements, order is
    important
  • Java also supplies some collection-like things
  • Map a dictionary that associates keys with
    values, order is not important
  • SortedMap like a Map, but order is important

4
Collections are ADTs
  • Im not going to cover Collections just yet, but
    I want to use them as an example
  • Collections are one of the best-designed parts of
    Java, because
  • They are elegant they combine maximum power with
    maximum simplicity
  • They are uniform when you know how to use one,
    you almost know how to use them all
  • You can easily convert from one to another

5
Uniformity through interfaces
  • Much of the elegance of the Collections Framework
    arises from the intelligent use of interfaces
  • For example, the Collection interface specifies
    (among many other operations)
  • boolean add(Object o)
  • boolean isEmpty()
  • boolean remove()
  • int size()
  • Object toArray()
  • Iterator iterator()

6
Vectors
  • The class Vector has been retrofitted to
    implement the Collection interface
  • Vector supplies add(Object) and iterator()
    methods (among others)
  • Lets look at creating a Vector and iterating
    through the elements

7
The Iterator interface
  • interface iterator // java.lang.util
  • boolean hasNext()
  • // Returns true if the iteration has more
    // elements.
  • Object next()
  • // Returns the next element in the //
    interation.
  • void remove()
  • // Removes from the underlying collection
    // the last element returned by the //
    iterator (optional operation).

8
Using a Vector
  • Collection numerals new Vector() numerals
    .add("one") numerals .add("two") numerals
    .add("three")
  • Iterator iter numerals.iterator() while
    (iter.hasNext()) System.out.println(iter.n
    ext())
  • Results one two three

9
Using a TreeSet
  • Collection numerals new TreeSet() numerals
    .add("one") numerals .add("two") numerals
    .add("three")
  • Iterator iter numerals.iterator() while
    (iter.hasNext()) System.out.println(iter.n
    ext())
  • Results one three two

10
Conversions
  • Vector v new Vector(numerals)
  • TreeSet ts new TreeSet(v)

11
Back to arrays
  • Arrays are not part of the new Collections
    Framework, but they havent been ignored
  • Java 1.2 introduced the new Arrays class, with
    some useful operations, for example
  • static void sort(Object a)
  • static int binarySearch(Object a, Object key)
  • static boolean equals(Object a, Object a2)
  • static void fill(Object a, Object val)
  • static void fill(Object a, Object val,
    int from, int to)
  • static List asList(Object a)

12
What to remember
  • We havent really begun to study the Collections
    framework yet, but
  • You should learn to use the Iterator interface
  • You should examine and learn more about the
    Arrays package

13
The End
Write a Comment
User Comments (0)
About PowerShow.com