The Collections API - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

The Collections API

Description:

a telephone directory (a collection of name-to-phone-number mappings) ... void reverse(List) void shuffle(List) void fill(List, Object) void copy(List dest, List src) ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 48
Provided by: elizabeth97
Category:

less

Transcript and Presenter's Notes

Title: The Collections API


1
The Collections API
  • http//java.sun.com/docs/books/tutorial/collection
    s/index.html
  • http//java.sun.com/j2se/1.5.0/docs/guide/collecti
    ons/index.html

2
What Is a Collection?
  • An object that groups multiple elements into a
    single unit.
  • Used to store, retrieve and manipulate data, and
    to transmit data from one method to another.
  • Typically represent data items that form a
    natural group, such as
  • a poker hand (a collection of cards),
  • a mail folder (a collection of letters), or
  • a telephone directory (a collection of
    name-to-phone-number mappings).

3
Why do we need collections?
  • Collections grow and shrink
  • An array size is final and cannot change
  • Collections can take in any data type
  • Arrays only allow classes of the same type
  • There are different types of collections with
    different semantics, such as defining uniqueness,
    sorting, etc.
  • Arrays are nothing more than lists

4
What Is a Collections Framework?
  • A unified architecture for representing and
    manipulating collections
  • All collections frameworks contain three things
  • Interfaces
  • Implementations
  • Algorithms
  • Examples of collections frameworks
  • the C Standard Template Library (STL)
  • Smalltalk's collection classes.
  • Javas collection framework

5
Benefits of a Collections Framework
  • Reduces programming effort
  • Increases program speed and quality
  • Interoperability among unrelated APIs
  • Reduces the effort to learn and use new APIs
  • Reduces effort to design new APIs
  • Fosters software reuse

6
Interfaces
  • The core collection interfaces are used to
    manipulate collections, and to pass them from one
    method to another.
  • Allow collections to be manipulated independently
    of the details of their representation.

7
Interfaces in the Collections Framework
Map
Collection
SortedMap
Set
List
SortedSet
8
Collection
  • java.util.Collection
  • Root of the collection hierarchy
  • Represents a group of objects, known as its
    elements.
  • Implementations differ
  • Some implementations allow duplicate elements and
    others do not.
  • Some are ordered and others unordered.
  • Collection interface is used to pass collections
    around and manipulate them when maximum
    generality is desired.

9
Collection
  • Commonly used methods
  • int size()
  • boolean isEmpty()
  • boolean contains(Object)
  • Iterator iterator()
  • Object toArray()
  • Object toArray(Object )
  • boolean add(Object)
  • boolean remove(Object)
  • void clear()

10
List
  • Interface java.util.List extends Collection
  • An ordered collection (sometimes called a
    sequence)
  • Similar to an array, but grows and shrinks!!
  • Can contain duplicate elements
  • Allows for precise control over where in the List
    each element is inserted
  • Access elements by their integer index (position)
  • Implementations
  • ArrayList
  • Vector
  • LinkedList

11
List Methods
  • Major additional methods (in addition to what it
    inherits from Collection)
  • Object get(int)
  • Object set(int, Object)
  • int indexOf(Object)
  • int lastIndexOf(Object)
  • void add(int, Object)
  • Object remove(int)
  • List subList(int, int)

12
List Implementations
List
LinkedList
  • Vector
  • a synchronized resizable-array implementation of
    a List with additional "legacy" methods.
  • ArrayList
  • a resizable-array implementation like Vector
  • unsynchronized (not thread safe)
  • May provide better performance than Vector
  • LinkedList
  • a doubly-linked list implementation
  • May provide better performance than ArrayList for
    frequent insertions/deletions
  • For queues and double-ended queues (deques)

Vector
ArrayList
13
Using a List
  • List sciences new Vector()
  • sciences.add(new Science("Computer Science"))
  • sciences.add(new Science("Biology"))
  • sciences.add(new Science("Chemistry"))
  • sciences.add(new Science("Physics"))
  • sciences.add(new Science("Astronomy"))
  • // eventhough I know that the list only contains
  • // Sciences, I have to cast it back because the
    get
  • // method returns Objects
  • Science cs (Science)sciences.get(0)
  • Science bio (Science)sciences.get(1)

14
Collection and Objects
  • The Collection methods take in Objects for
    maximum flexibility
  • All classes extend Object, so anything can be
    added to a Collection
  • If a primitive is added, autoboxing will occur
    converting it to the wrapper type
  • This allows you to put objects of different types
    in the same collection
  • Although the semantics of collections which
    deal with sorting may result in a class cast
    exception if data types are mixed

15
Using a List
  • List sciences new Vector()
  • sciences.add(new Science("Computer Science"))
  • sciences.add(new Science("Biology"))
  • sciences.add(5) // autoboxed to new Integer(5)
  • sciences.add(true) // autoboxed to Boolean.TRUE
  • sciences.add("Astronomy")
  • // what happens when I run the following?
  • Science cs (Science)sciences.get(0)
  • Science bio (Science)sciences.get(1)
  • Integer five (Integer)sciences.get(2)
  • int f (Integer)sciences.get(2)
  • Science astronomy (Science)sciences.get(4)

16
Vectors and Objects
  • Notice that when dealing with the Vector class,
    it takes in Objects and returns Objects.
  • When retrieving elements from the list, it has to
    be casted back to the appropriate data type at
    runtime.
  • List sciences new Vector()
  • sciences.add(new Science("Physics"))
  • Science s (Science)sciences.get(0)
  • Problems
  • Incovenient - have to write casting code in order
    to compile
  • Unsafe - may result in a ClassCastException at
    runtime if the objects in the list are of a
    different type

17
Generics
  • To the most despised cast
  • We'll bid a fond farewell at last
  • With generics' burning spear
  • The need for cast will disappear
  • -Joshua Bloch

18
Generics
  • Were added in 1.5
  • Provide type safety compile time checking of
    types when using collection classes
  • Let's take another look at the Vector class
    documentation. Notice the Vector?
  • The E gets replaced with the name of a class or
    interface.
  • List sciences
  • sciences new Vector()
  • If using generics, any methods that return E will
    actually return a Science object (no casting
    needed)
  • sciences.add(new Science("Physics"))
  • Science s sciences.get(0)

http//java.sun.com/j2se/1.5.0/docs/guide/language
/generics.html
19
How Generics Work
  • Generics are implemented by type erasure
  • Generic type information is present only at
    compile time, after which it is erased by the
    compiler.
  • This allows for interoperability between generic
    code and legacy code that uses non-parameterized
    types (aka raw types).
  • The down side?
  • Parameter type information is not available at
    run time
  • You can still have class cast problems with
    legacy code
  • Not compatible with 1.4

20
List example without generics
  • List players
  • players new ArrayList()
  • players.add(new Player("Rodriguez", 13))
  • players.add(new Player("Jeter", 2))
  • players.add(new Player("Jeter", 2))
  • for (int i 0 i
  • Player p (Player)players.get(i)
  • System.out.println(p)
  • // retrieves players in the order in which
  • // they were added, Rodriguez, Jeter, Jeter
  • // duplicates are retained

21
List example using generics
  • List players
  • players new ArrayList()
  • players.add(new Player("Rodriguez", 13))
  • players.add(new Player("Jeter", 2))
  • players.add(new Player("Jeter", 2))
  • for (int i 0 i
  • Player p players.get(i)
  • System.out.println(p)
  • // retrieves players in the order in which
  • // they were added, Rodriguez, Jeter, Jeter
  • // duplicates are retained

22
Set
  • Interface java.util.Set extends Collection
  • Models the mathematical set abstraction
  • Unordered collection of objects
  • No duplicate elements
  • Implementations
  • HashSet
  • TreeSet

23
Set Implementations
  • HashSet
  • a Set backed by a hash table
  • TreeSet
  • Implementation of a balanced binary tree
  • Imposes an ordering on its elements

Set
SortedSet
HashSet
TreeSet
24
Set example
  • Set jerseyNumbers
  • jerseyNumbers new TreeSet()
  • jerseyNumbers.add(new Integer(13))
  • jerseyNumbers.add(new Integer(2))
  • jerseyNumbers.add(new Integer(2))
  • // duplicates were removed, TreeSet is ordered
  • // there's no get method to retrieve 1 element ?
  • Integer all new IntegerjerseyNumber.size()
  • jerseyNumbers.toArray(all)
  • for (int i 0 i
  • System.out.print(alli " ")
  • // output 2 13

25
Map
  • Interface java.util.Map
  • Does not extend Collection!
  • An object that maps keys to values
  • Each key can have at most one value
  • Ordering may be provided by implementation class,
    but not guaranteed

26
Map Details
  • Major methods
  • int size()
  • boolean isEmpty()
  • boolean containsKey(Object)
  • boolean containsValue(Object)
  • Object get(Object)
  • Object put(Object, Object)
  • Object remove(Object)
  • void putAll(Map)
  • void clear()
  • Implementations
  • HashMap, Hashtable, WeakHashMap, Attributes

27
Accessing all members of Map
  • Methods
  • Set keySet()
  • Collection values()
  • Set entrySet()
  • Map.Entry
  • Object that contains a key-value pair
  • getKey()
  • getValue()
  • Thread safety
  • The collections returned are backed by the map
  • When the map changes, the collection changes
  • Behavior can easily become undefined
  • Be very careful and read the docs closely

28
Map Implementations
  • HashMap
  • A hash table implementation of Map
  • Like Hashtable, but supports null keys values
  • TreeMap
  • A balanced binary tree implementation
  • Imposes an ordering on its elements
  • Hashtable
  • Synchronized hash table implementation of Map
    interface, with additional "legacy" methods.

Map
Hashtable
HashMap
SortedMap
TreeMap
29
Map and generics
  • Since a map deals with both a key and value, the
    declaration loops like Map
  • So, when you create a Map using generics, you
    need to specify the type of key and type of
    value
  • Map map
  • map.put(new Integer(1), "Hey there!")

30
Map Example
  • Map players
  • players new TreeMap()
  • // relies on autoboxing for the keys
  • players.put(13, new Player("Rodriguez", 13))
  • players.put(2, new Player("Jeter", 2))
  • players.put(2, new Player("Unknown", 2))
  • Player p
  • p players.get(13)
  • System.out.println(p) // Rodriguez
  • p players.get(2)
  • System.out.println(p) // Unknown
  • // duplicates removed, TreeMap is ordered on key
  • // 2nd call to put with key 2 replaces the first
  • p players.get(3)

31
Which type of collection would you use?
  • Lines in a log file
  • A deck of cards
  • A Stack
  • LIFO data structure
  • A team of players
  • Can lookup a player by Jersey Number
  • A class roster
  • Dictionary entries

Map
List
Set
Sorted Set
32
Utilities Used by the Collections Framework
Interfaces
Iterator
Comparator
ListIterator
Classes
Collections
Arrays
33
Iterator
  • java.util.Iterator
  • ADT representation of a loop
  • Created by Collection.iterator()
  • Methods
  • boolean hasNext()
  • Object next()
  • void remove()
  • Removes the element from the underlying
    collection

34
Iterator and List example
  • List players
  • players new ArrayList()
  • players.add(new Player("Rodriguez", 13))
  • players.add(new Player("Jeter", 2))
  • players.add(new Player("Jeter", 2))
  • Iterator i players.iterator()
  • while (i.hasNext())
  • Player p i.next()
  • System.out.println(p)
  • // retrieves players in the order in which
  • // they were added, Rodriguez, Jeter, Jeter
  • // duplicates are retained

35
Iterator and Set example
  • Set jerseyNumbers
  • jerseyNumbers new TreeSet()
  • jerseyNumbers.add(new Integer(13))
  • jerseyNumbers.add(new Integer(2))
  • jerseyNumbers.add(new Integer(2))
  • Iterator i players.iterator()
  • while (i.hasNext())
  • Integer i i.next()
  • System.out.println(i)
  • // output 2 13
  • // duplicates removed, TreeSet is ordered

36
Iterator and Map Example
  • Map players
  • players new TreeMap()
  • players.put(new Integer(13),
  • new Player("Rodriguez", 13))
  • players.put(new Integer(2), new Player("Jeter",
    2))
  • players.put(new Integer(2), new Player("Unknown",
    2))
  • Set keySet players.keySet()
  • Iterator i keySet.iterator()
  • while (i.hasNext())
  • Player p players.get(i.next())
  • System.out.println(p)
  • // output
  • Unknown
  • Rodriguez
  • // duplicates removed, TreeMap is ordered on key
  • // 2nd put with key 2 replaces the first

37
Enhanced for loop
  • While Iterators have their uses
  • They sometimes strangle us like nooses
  • With enhanced-for's deadly ray
  • Iterator's kept at bay
  • - Joshua Bloch

38
Enhanced for loop
  • aka the "foreach" loop
  • Lets you iterate over collections without using
    Iterators or index variables
  • Both can be error prone

39
Errors with Iterators
  • Iterator i roster.iterator()
  • while (i.hasNext())
  • System.out.print(
  • i.next().getJerseyNumber())
  • System.out.print(" - ")
  • System.out.println(i.next().getName())
  • // What is the problem?

40
Errors with index variables
  • int n1 ...
  • for (int i 0 i
  • System.out.println(n1i)
  • int n2 ...
  • int j 0
  • while (j
  • System.out.println(n2j)

41
Using the enhanced for loop
  • for (Player player roster)
  • System.out.print(player.getJerseyNumber())
  • System.out.print(" - ")
  • System.out.println(player.getName())
  • int n1 ...
  • for (int i n1)
  • System.out.println(i)

42
Nested loops
  • The enhanced for loop is perfect for nested
    loops!
  • for (Suit suit suits)
  • for (Rank rank ranks)
  • sortedDeck.add(new Card(suit, rank))

43
Utility Classes - Collections
  • Collections class (note the s)
  • Static methods
  • void sort(List)
  • int binarySearch(List, Object)
  • void reverse(List)
  • void shuffle(List)
  • void fill(List, Object)
  • void copy(List dest, List src)
  • Object min(Collection c)
  • Object max(Collection c)
  • synchronizedX, unmodifiableX factory methods

44
Utility Classes - Arrays
  • Arrays class
  • Static methods that act on Java arrays
  • sort
  • binarySearch
  • equals
  • fill
  • asList - returns an ArrayList composed of this
    array's contents

45
Sorting
  • Collections.sort(...) static method
  • SortedSet, SortedMap interfaces
  • Collections that keep their elements sorted
  • Iterators are guaranteed to traverse in sorted
    order
  • Ordered Collection Implementations
  • TreeSet, TreeMap

46
Sorting
  • java.lang.Comparable interface
  • Must be implemented by all elements in SortedSet
  • Must be implemented by all keys in SortedMap
  • Method int compareTo(Object o)
  • Defines "natural order" for that object class
  • java.util.Comparator interface
  • Defines a function that compares two objects
  • Can design custom ordering scheme
  • Method int compare(Object o1, Object o2)

47
Sorting
  • Sorting Arrays
  • Use Arrays.sort(Object)
  • Objects must implement the Comparable interface
  • Equivalent methods for all primitive types
  • Arrays.sort(int)
  • Arrays.sort(double), etc.
Write a Comment
User Comments (0)
About PowerShow.com