Java Collections: Built-in Data Structures for Java - PowerPoint PPT Presentation

1 / 59
About This Presentation
Title:

Java Collections: Built-in Data Structures for Java

Description:

Java Collections: Built-in Data Structures for Java Cheng-Chia Chen – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 60
Provided by: bruc1213
Category:

less

Transcript and Presenter's Notes

Title: Java Collections: Built-in Data Structures for Java


1
Java CollectionsBuilt-in Data Structures for
Java
  • Cheng-Chia Chen

2
  • If your are not familiar with any of the
    following terms
  • List, Stack, Queue, Tree, Heap, hashtable
  • Please review your data structure book or
    read chapter 20 of the textbook carefully for
    topics related to these terms.

3
The Java Collection API
  • Interfaces
  • Collection (Iterable) ?
  • Set ? SortedSet,
  • List
  • Queue
  • Map ? SortedMap
  • Iterator ? ListIterator
  • Comparator
  • Note After JDK5, all of above interfaces (and
    their implementations mentioned hereafter )
    become generic types and hence are parameterized
    by a type variable ltEgt. I.e., Formally they
    should be CollectionltEgt, SetltEgt, , etc.

Queue
4
Summary of all interfaces in the java Collection
API
  • Collection Interfaces The primary means by
    which collections are manipulated.
  • ( IterableltEgt ? ) CollectionltEgt ? SetltEgt, ListltEgt
  • A group of objects.
  • May or may not be ordered May or may not
    contain duplicates.
  • SetltEgt ? SortedSetltEgt
  • The familiar set abstraction.
  • No duplicates May or may not be ordered.
  • SortedSet ltEgt
  • elements automatically sorted, either in their
    natural ordering (see the Comparable interface),
    or by a Comparator object provided when a
    SortedSet instance is created.
  • ListltEgt
  • Ordered collection, also known as a sequence.
  • Duplicates permitted Allows positional access.

5
  • QueueltEgt ? DequeltEgt ? BlockingQueueltEgt
  • a First-in/First-out data container. Elements are
    appended to the ends of the queue and are removed
    from the beginning of the queue.
  • deque are double-ended queue.
  • MapltK,Vgt ?SortedMapltK,Vgt
  • A mapping from keys to values.
  • Each key can map to at most one value (function).
  • SortedMap ltK,Vgt
  • A map whose mappings are automatically sorted by
    key, either in the keys' natural ordering or by a
    comparator provided when a SortedMap instance is
    created.

6
Infrastructure
  • Iterators Similar to the Enumeration
    interface, but more powerful, and with improved
    method names.
  • IteratorltEgt ? ListIteratorltEgt
  • functionality of the Enumeration interface
    next() E hasNext() boolean remove()
  • supports element removement from the backing
    collection.
  • ListIteratorltEgt
  • Iterator for use with lists.
  • supports bi-directional iteration, element
    replacement, element insertion and index
    retrieval.
  • Ordering
  • Comparable ltTgt ( compareTo( T object ) )
  • Imparts a natural ordering to classes that
    implement it.
  • The natural ordering may be used to sort a list
    or maintain order in a sorted set or map. Many
    classes were designed to implement this
    interface.
  • ComparatorltTgt ( compare(T o1, T o2 )
    equals(Object obj ) )
  • Represents an order relation, which may be used
    to sort a list or maintain order in a
    sorted set or map. Can override a type's natural
    ordering, or order objects of a type that does
    not implement the Comparable interface.

7
Infrastructure
  • Runtime Exceptions
  • UnsupportedOperationException
  • Thrown by collections if an unsupported optional
    operation is called.
  • ConcurrentModificationException
  • Thrown by Iterator and listIterator if the
    backing collection is modified unexpectedly while
    the iteration is in progress.
  • Also thrown by sublist views of lists if the
    backing list is modified unexpectedly.

8
classes of the java collection API
  • AbstractCollection (Collection) ?
  • AbstractSet (Set) ? TreeSet(SortedSet), HashSet ?
    LikedHashSet
  • AbstractList (List) ?ArrayList,
    Vector? Stack,

  • AbstractSequentialList ? LinkedList (Deque)
  • AbstractQueue(Queue) ? PriorityQueue
  • AbstractMap (Map) ?
  • HashMap, Hashtable
  • TreeMap (SortedMap)
  • WeakHashMap
  • Arrays
  • Collections
  • Legends 1. superclass (interface) ? subclass
  • 2. red interface brown
    abstract blue concrete

9
General-Purpose Implementation classes
  • The primary implementations of the collection
    interfaces.
  • HashSet Hash table implementation of the Set
    interface.
  • TreeSet Red-black tree implementation of the
    SortedSet interface.
  • ArrayList Resizable-array implementation of
    the List interface.
  • (Essentially an unsynchronized Vector.) The
    best all-around implementation of the List
    interface.
  • LinkedList Doubly-linked list implementation of
    the List interface.
  • May provide better performance than the ArrayList
    implementation if elements are frequently
    inserted or deleted within the list.
  • Useful for queues and double-ended queues
    (dequeues).
  • HashMap Hash table implementation of the Map
    interface.
  • (Essentially an unsynchronized Hashtable that
    supports null keys and values.) The best
    all-around implementation of the Map
    interface.
  • TreeMap Red-black tree implementation of the
    SortedMap interface.

10
The java.util.Collection interface
  • represents a group of objects, known as its
    elements.
  • no direct implementation
  • The primary use pass around collections of
    objects where maximum generality is desired.
  • Ex List l new ArrayList( c ) // c is a
    Collection object
  • or ListltEgt new ArrayListltEgt( c )
  • // E any reference Type c
    Collectionlt? extends E1gt

11
The definition
  • public interface Collection ltEgt extends Iterable
    ltEgt
  • // Basic properties
  • int size()
  • boolean isEmpty()
  • boolean contains(Object element) // use
    equals() for comparison
  • boolean equals(Object)
  • int hashCode() // new equals() requires new
    hashCode()
  • // basic operations
  • boolean add(E e) // Optional return
    true if this changed
  • boolean remove(E e) // Optional use equals()
    (not )

12
The Collection interface definition
  • // Bulk Operations
  • boolean containsAll(Collection lt?gt c) //
    true if c ? this
  • // the following 4 methods are optional,
    returns true if contents changed
  • boolean addAll(Collectionlt? extends Egt c)
    // Optional this this U c
  • boolean removeAll(Collectionlt?gt c) //
    Optional this this - c
  • boolean retainAll(Collectionlt?gt c) //
    Optional this this ?c
  • void clear() // Optional
    this
  • // transformations
  • IteratorltEgt iterator() // collection ?
    iterator imple. of Iterable
  • Object toArray() // collection ?
    array
  • ltTgt T toArray(T a ) // generic
    method with type ltTgt, not ltEgt
  • // if this a gt copy this to a and
    return a else gt create a new array b
  • // of the type T and copy this to b and
    return b

13
Why using Iterators instead of Enumerations
  • Method names are simpler.
  • Iterator allows the caller to remove elements
    from the underlying collection.
  • public interface Iterator ltEgt
  • boolean hasNext() // cf
    hasMoreElements()
  • E next() //
    cf nextElement()
  • void remove() // Optional
  • // a simple Collection filter using iterator that
    Enumeration could not help
  • static ltTgt void filter(CollectionltTgt c)
  • for (IteratorltTgt i c.iterator()
    i.hasNext() )
  • if ( no-good( i.next() ) )
    i.remove() // i.next() is removed from c
  • i.remove() // exception
    raised, cannot removed more than once !

14
Ex ( Use raw type)
  • Integer a1 new Integer 1,2,3,4.
  • List c Arrays.asList( a1 ) // c 1,2,3,4
  • Iterator l c.iterator()
  • While( l.hasNext() )
  • Integer k (Integer) l.next()
  • if ( k .intValue 3) l.remove()
  • c.println() // c 1,2,4

15
Ex (use generic type)
  • Integer a1 new Integer 1,2,3,4.
  • List ltIntegergt c Arrays.asList( a1 ) // c
    1,2,3,4
  • Iterator ltIntegergt l c.iterator()
  • While( l.hasNext() )
  • Integer k (Integer) l.next() // cast not
    needed
  • if ( k .intValue 3) l.remove()
  • c.println() // c 1,2,4

16
Examples of Bulk and array operations for
Collection
  • Remove all instances of an element e from a
    collection c
  • c.removeAll(Collections.singleton(e)) // remove
    all es in c
  • c.removeAll(Collections.singleton(null)) //
    remove all nulls in c
  • collections to arrays
  • Collection c new LinkedList() // LinkedList
    is an imp. of Col.
  • c .add(a) c.add(b) // c a, b
    component type Object
  • Object ar1 c.toArray() // ok, ar1.length
    2
  • String ar2 (String) c.toArray() // may
    cause runtime
  • // exception, cannot cast an array of
    Object component type
  • // to String note can pass compilation.
  • String ar3 (String) c.toArray(new
    String0) // ok! since c.toArray(String) has
    String component type.

17
Examples of Bulk and array operations for
Collection
  • collections to arrays (use generic type)
  • Collection c new LinkedListltStringgt()
  • c .add(a) c.add(b) // c a, b
    component type Object
  • Object ar1 c.toArray() // ar1.length 2
  • String ar2 (String) c.toArray()
  • String ar3 (String) c.toArray(new
    String0)
  • // still need
    pass a String.

18
The Set Interface
  • is a Collection that cannot contain duplicate
    elements.
  • models the mathematical set abstraction.
  • contains no methods other than those inherited
    from Collection.
  • same signatures but different semantics ( meaning
    )
  • Collection c new LinkedList() Set s new
    HashSet()
  • String o a
  • c.add(o) c.add(o) // both return true
    c.size() 2
  • s.add(o) s.add(o) // 2nd add() returns false
    s.size() 1
  • It adds the restriction that duplicate elements
    are prohibited.
  • Collection noDups new HashSet(c) // a simple
    way to eliminate duplicate from c
  • Two Set objects are equal if they contain the
    same elements.
  • Two direct implementations
  • HashSet TreeSet

19
Basic Operations
  • A simple program to detect duplicates using set
  • import java.util.
  • public class FindDups
  • public static void main(String args)
  • Set s new HashSet() // or use new
    TreeSet()
  • // following code uses Set methods only
  • for (String arg args )
  • if ( !s.add(arg) ) out.println("Duplicate
    detected arg )
  • out.printf( s.size() distinct words detected
    s )
  • java FindDups i came i saw i left
  • Duplicate detected i
  • Duplicate detected i
  • 4 distinct words detected came, left, saw,
    i

20
Bulk Operations for Set objects
  • s1.containsAll(s2) returns true if s2 is a subset
    of s1.
  • s1.addAll(s2), s1.retainAll(s2),
    s1.removeAll(s2)
  • s1 s1 U s2, s1 s1 ? s2, s1 s1 s2,
    respectively
  • return true iff s1 is modified.
  • For nondestructive operations
  • Set union new HashSet(s1) // make a copy of
    s1
  • union.addAll(s2)
  • Set intersection new HashSet(s1) // may also
    use TreeSet(s1)
  • intersection.retainAll(s2)
  • Set difference new HashSet(s1)
  • difference.removeAll(s2)

21
Example
  • Show all arguments that occur exactly once and
    those that occur more than once.
  • import java.util. //use generics
  • public class FindDups2
  • public static void main(String args)
  • Set ltStringgt uniques new HashSet
    ltStringgt(),
  • dups new HashSet ltStringgt ()
  • for (String arg args )
  • if (! uniques.add(arg))
    dups.add(arg )
  • uniques.removeAll(dups) // Destructive
    set-difference
  • out.println("Unique words "
    uniques)
  • out.println("Duplicate words " dups)

22
The List Interface
  • A List is an ordered Collection(sometimes called
    a sequence).
  • may contain duplicate elements.
  • The List interface includes operations for
  • Positional Access set/get elements based on
    their numerical position in the list.
  • Search search for a specified object in the list
    and return its numerical position.
  • List Iteration extend Iterator semantics to take
    advantage of the list's sequential nature.
  • Range-view perform arbitrary range operations on
    the list.
  • Three direct implementations
  • ArrayList resizable array
  • LinkedList doubly linked-list
  • Vector synchronized ArrayList.

23
The List interface definition
  • public interface List ltEgt extends CollectionltEgt
  • // Positional Access
  • E get(int index) // 0-based
  • E set(int index, E e) //
    Optional return old value
  • void add(int index, E e) //
    Optional
  • E remove(int index) // Optional
    remove(Object) given in Collection
  • boolean addAll(int index, Collectionlt? extends
    Egt c) // Optional
  • // Search
  • int indexOf(Object o) // the type
    of argument is Object!
  • int lastIndexOf(Object o)
  • // Range-view
  • ListltEgt subList(int from, int to)
  • // Iteration Iterator iterator() given in
    Collection
  • ListIteratorltEgt listIterator(int f ) //
    default f 0 return a listIterator with cursor
    set to position f

24
List in comparison with Older Vector (1.1)
  • shorter getter/setter names
  • get(int) // elementAt(int) /
  • add( int, Object) //
    insertElemetnAt(O,i)
  • Object set(int, Object) // void
    setElementAt(Object,int)
  • Object remove(int) // void
    removeElementAt(int)
  • Note From java1.2 Vector also implements the
    List interface.
  • List concatenation
  • list1 list1.addAll(list2) // destructive
  • List list3 new arrayList( list1) // or
    LinkedList(list1)
  • list3.addAll(list2) // list3 equals to list1 .
    list2
  • Two List objects are equal if they contain the
    same elements in the same order.
  • List l1 new LinkedList(l2) // l2 is an
    ArrayList
  • l1.equals(l2) ? true false // returns true, but
    l1l2 returns false.

25
ListIterator
  • public interface ListIteratorltEgt extends
    IteratorltEgt
  • // from Iterator (forward iteration)
  • boolean hasNext()
  • E next()
  • // backward iteration
  • boolean hasPrevious()
  • E previous()
  • int nextIndex() // cursor position
    pos of next() object
  • int previousIndex() // nextIndex()
    1 pos of previous() object
  • //
    -1 if cursor 0
  • void remove() // Optional
  • void set(E e) // Optional
  • void add(E e) // Optional

26
Set and Add operations in ListIterator
  • set(E), remove() Replaces/remove the last
    element returned by next or previous with the
    specified element.
  • Ex gt either E(1) (if next() is called more
    recently than previous())
  • or E(2) (otherwise)
  • would be replaced
  • add(Object o ) Inserts the specified element
    into the list, immediately before the current
    cursor position.
  • Ex add(o) gt E(0) E(1) o (cursor) E(2)
    E(3)
  • Backward Iteration
  • for(ListIterator i list.listIterator(list.siz
    e()) i.hasPrevious() ) processing(
    i.previous())

E(0) E(1) E(2)
E(3)
Index 0 1
2 3 4
(cursor)
27
Some Examples
  • possible implementation of List.indexOf(Object)
  • public int indexOf(Object o)
  • for (ListIterator i listIterator()
    i.hasNext() )
  • if (onull ? i.next()null
    o.equals(i.next()))
  • return i.previousIndex() //
    or i.nextIndex() -1
  • return -1 // Object not found
  • replace all occurrences of one specified value
    with another
  • public static void replace(List l, Object val,
    Object newVal)
  • for (ListIterator i l.listIterator()
    i.hasNext() )
  • if (valnull ? i.next()null
    val.equals(i.next()))
  • i.set(newVal)

28
Range-view Operation
  • subList(int f, int t), returns a List view of a
    portion of this list whose indices range from f,
    inclusive, to t, exclusive, f,t).
  • Ex
  • sublist(1,3) E(1),E(2)
  • This half-open range mirrors the typical
    for-loop
  • for (int if iltt i) ...
    // iterate over the sublist
  • Change on sublist is reflected on the backing
    list
  • Ex the following idiom removes a range of
    elements from a list
  • list . subList(from, to) . clear()

E(0) E(1) E(2)
E(3)
Index 0 1
2 3 4
(cursor)
29
The Map Interface
  • A Map is an object that maps keys to values.
  • A map cannot contain duplicate keys
  • Each key can map to at most one value.
  • Three implementations
  • HashMap, which stores its entries in a hash
    table, is the best-performing implementation.
  • TreeMap, which stores its entries in a red-black
    tree, guarantees the order of iteration.
  • Hashtable has also been retrofitted to implement
    Map.
  • All implementation must provide two constructors
    (like Collections) Assume M is your
    implementation
  • M() // empty map
  • M(Maplt? extends K, ? extends Vgt m) // a copy of
    map from m

30
The Map interface
  • public interface MapltK,Vgt // Map does not
    extend Collection
  • // Basic Operations
  • // put or replace, return replaced object
  • V put(K key, V value) // optional old value
    or null returned
  • V get(K key)
  • V remove(Object key)
  • boolean containsKey(Object key)
  • boolean containsValue(Object value)
  • int size()
  • boolean isEmpty()

31
The Map interface
  • // Bulk Operations
  • void putAll(Maplt?extends K, ? extends Vgt
    t) //optional
  • void clear() // optional
  • // Collection Views
  • // backed by the Map, change on either
    will be reflected on the other.
  • public SetltKgt keySet() // cannot
    duplicate by definition!!
  • public CollectionltVgt values() //
    can duplicate
  • public SetltMap.EntryltK,Vgtgt
    entrySet() // no equivalent in Dictionary
  • // nested Interface for entrySet elements
  • public interface EntryltK,Vgt
    equals(Object) hashCode()
  • K getKey()
  • V getValue()
  • V setValue(V value)

32
Possible Exceptions thrown by Map methods
  • UnsupportedOperationException
  • if the method is not supported by this map.
  • ClassCastException
  • if the class of a key or value in the specified
    map prevents it from being stored in this map.
  • ex m.put(Name, new Integer(2)) // m actually
    put (String) value
  • IllegalArgumentException
  • some aspect of a key or value in the specified
    map prevents it from being stored in this map.
  • ex put(Two, 2) // put expect s an Object
    value
  • NullPointerException
  • this map does not permit null keys or values,
    and the specified key or value is null.

33
Basic Operations
  • a simple program to generate a frequency table
  • import java.util.
  • public class Freq private static final
    Integer ONE new Integer(1)
  • public static void main(String args)
  • Map m new HashMap() // or
    MapltString, Integergt m
  • // Initialize frequency table from
    command line
  • for (int i0 iltargs.length i)
  • Integer freq (Integer)
    m.get(argsi) // key is a string
  • m.put(argsi, (freqnull ? ONE new
    Integer( freq.intValue() 1)))
  • // value is Integer
  • System.out.println( m.size()" distinct
    words detected")
  • System.out.println(m)
  • gt java Freq if it is to be it is up to me to
    delegate
  • 8 distinct words detected
  • to3, me1, delegate1, it2, is2,
    if1, be1, up1

34
Basic Operations ( after JDK 1.5)
  • a simple program to generate a frequency table
  • import java.util.
  • public class Freq private static final int
    ONE 1
  • public static void main(String args)
  • MapltString, Integergt m new
    HashMapltString, Integergt()
  • // Initialize frequency table from
    command line
  • for (String arg args)
  • Integer freq m.get(arg)
  • m.put( arg, (freqnull ? ONE freq
    1)) // autoBox, autoUnbox used
  • // value is Integer
  • System.out.println( m.size()" distinct
    words detected")
  • System.out.println(m)
  • gt java Freq if it is to be it is up to me to
    delegate
  • 8 distinct words detected
  • to3, me1, delegate1, it2, is2,
    if1, be1, up1

35
Bulk Operations
  • clear() removes all of the mappings from the
    Map.
  • putAll(Map) operation is the Map analogue of the
    Collection interface's addAll() operation.
  • can be used to create attribute map creation
    with default values. Here's a static factory
    method demonstrating this technique
  • static ltK,Vgt MapltK,Vgt newAttributeMap(MapltK,Vgt
    defaults, Maplt?extends K, V extends Vgt overrides)
  • MapltK,Vgt result new HashMapltK,Vgt
    (defaults)
  • result.putAll(overrides)
  • return result

36
Collection Views methods
  • allow a Map to be viewed as a Collection in three
    ways
  • keySet the Set of keys contained in the Map.
  • values The Collection of values contained in the
    Map. This Collection is not a Set, as multiple
    keys can map to the same value.
  • entrySet The Set of key-value pairs contained in
    the Map.
  • The Map interface provides a small nested
    interface called Map.Entry that is the type of
    the elements in this Set.
  • the standard idiom for iterating over the keys in
    a Map
  • for (Iterator i m.keySet().iterator()
    i.hasNext() )
  • System.out.println(i.next())
  • if(no-good()) i.remove() //
    support removal from the back Map
  • Iterating over key-value pairs
  • for (Iterator im.entrySet().iterator()
    i.hasNext() )
  • Map.Entry e (Map.Entry) i.next()
  • System.out.println(e.getKey() " "
    e.getValue())

37
Permutation groups of words
  • import java.util. import java.io.
  • public class Perm
  • public static void main(String args)
  • int minGroupSize Integer.parseInt(args1)
  • // Read words from file and put into
    simulated multimap
  • MapltString, ListltStringgtgt m new
    HashMapltString, ListltStringgt ()
  • try Scanner in new Scanner(new
    File(args0))
  • while(in.hasNext())
  • String word in.nextLine()
    // or word in.next()
  • String alpha
    alphabetize(word)
  • //
    normalize word success? ccesssu
  • List ltStringgt l
    m.get(alpha)
  • if (lnull) m.put(alpha,
    lnew ArrayListltStringgt () )
  • l.add(word)
  • catch(Exception e)
    System.err.println(e) System.exit(1)

38
  • // Print all permutation groups above size
    threshold
  • for (IteratorltListltStringgtgt i
    m.values().iterator() i.hasNext() )
  • ListltStringgt l (List) i.next()
  • if (l.size() gt minGroupSize)
  • System.out.println(l.size() " "
    l)

39
  • // buketsort implementation
  • private static String alphabetize(String s)
  • int count new int256
  • int len s.length()
  • for (int i0 iltlen i)
  • counts.charAt(i)
  • StringBuffer result new
    StringBuffer(len)
  • for (char c'a' clt'z' c)
  • for (int i0 iltcountc
    i)
  • result.append(c)
  • return result.toString()

40
Some results
  • java Perm dictionary.txt 8
  • 9 estrin, inerts, insert, inters, niters,
    nitres, sinter, triens, trines
  • 8 carets, cartes, caster, caters, crates,
    reacts, recast, traces
  • 9 capers, crapes, escarp, pacers, parsec,
    recaps, scrape, secpar, spacer
  • 8 ates, east, eats, etas, sate, seat, seta,
    teas
  • 12 apers, apres, asper, pares, parse, pears,
    prase, presa, rapes, reaps, spare, spear
  • 9 anestri, antsier, nastier, ratines, retains,
    retinas, retsina, stainer, stearin
  • 10 least, setal, slate, stale, steal, stela,
    taels, tales, teals, tesla
  • 8 arles, earls, lares, laser, lears, rales,
    reals, seral
  • 8 lapse, leaps, pales, peals, pleas, salep,
    sepal, spale
  • 8 aspers, parses, passer, prases, repass,
    spares, sparse, spears
  • 8 earings, erasing, gainers, reagins, regains,
    reginas, searing, seringa
  • 11 alerts, alters, artels, estral, laster,
    ratels, salter, slater, staler, stelar, talers
  • 9 palest, palets, pastel, petals, plates,
    pleats, septal, staple, tepals

41
The SortedSet Interface
  • A SortedSet is a Set that maintains its elements
    in ascending order, sorted according to the
    elements' natural order(via comparable
    interface), or according to a Comparator provided
    at SortedSet creation time.
  • In addition to the normal Set operations, the
    SortedSet interface provides operations for
  • Range-view Performs arbitrary range operations
    on the sorted set.
  • Endpoints Returns the first or last element in
    the sorted set.
  • Comparator access Returns the Comparator used to
    sort the set (if any).
  • Standard constructors Let S be the
    implementation
  • S( Comparator ) // empty set
  • S(SortedSet) // copy set
  • Implementation TreeSet

42
The SortedSet
  • public interface SortedSetltEgt extends SetltEgt
  • // Range-view
  • SortedSetltEgt subSet(E f, E t) // return f, t
    ), f.eq(t) -gtnull
  • SortedSetltEgt headSet(E t) //
    first(), t )
  • SortedSetltEgt tailSet(E fromElement)// f,
    last()
  • // Endpoints
  • E first()
  • E last()
  • // Comparator access
  • Comparatorlt? super Egt comparator() //
    return null if natural order is used.

43
The SortedMap Interface
  • A SortedMap is a Map that maintains its entries
    in ascending order, sorted according to the keys
    natural order, or according to a Comparator
    provided at SortedMap creation time.
  • In addition to the normal Map operations, the
    SortedMap interface provides operations for
  • Range-view Performs arbitrary range operations
    on the sorted map.
  • Endpoints Returns the first or last key in the
    sorted map.
  • Comparator access Returns the Comparator used to
    sort the map (if any).
  • Constructors provided by implementations
  • M(Comparator) // empty SortedMap
  • M(SortedMap) // copy Map
  • Implementation TreeMap

44
The SortedMap interface
  • // analogous to SortedSet
  • public interface SortedMapltK,Vgt extends MapltK,Vgt
  • Comparatorlt? super Kgt comparator()
  • // range-view operations
  • SortedMapltK,Vgt subMap(K fromKey, K
    toKey)
  • SortedMapltK,Vgt headMap(K toKey)
  • SortedMapltK,Vgt tailMap(K fromKey)
  • // member access
  • // Dont forget bulk of other Map operations
    available
  • K firstKey()
  • K lastKey()
  • // throws NoSuchElementException if
    m.isEmpty()

45
SortedMap Operations
  • Operations inheriting from Map behave identically
    to normal maps with two exceptions
  • keySet() entrySet() values() . Iterator()
    traverse the collections in key-order.
  • toArray() contains the keys, values, or entries
    in key-order.
  • Although not guaranteed by the interface,
  • the toString() method of SortedMap in all the
    JDK's SortedMap returns a string containing all
    the elements in key-order.

46
Example
  • SortedMapltString,Stringgt m new
    TreeMapltString,Stringgt()
  • m.put("Sneezy", "common cold")
  • m.put("Sleepy", "narcolepsy")
  • m.put("Grumpy", "seasonal affective
    disorder")
  • System.out.println( m.keySet() )
  • System.out.println( m.values() )
  • System.out.println( m.entrySet() )
  • Running this snippet produces this output
  • Grumpy, Sleepy, Sneezy
  • seasonal affective disorder, narcolepsy, common
    cold
  • Grumpyseasonal affective disorder,
    Sleepynarcolepsy,
  • Sneezycommon cold

47
Actual Collection and Map Implementations
  • Implementations are the actual data objects used
    to store collections (and Maps).
  • Three kinds of implementations
  • General-purpose Implementations
  • the public classes that provide the primary
    implementations of the core interfaces.
  • Wrapper Implementations
  • used in combination with other implementations
    (often the general-purpose implementations) to
    provide added functionality.
  • Convenience Implementations
  • Convenience implementations are
    mini-implementations, typically made available
    via static factory methods that provide
    convenient, efficient alternatives to the
    general-purpose implementations for special
    collections (like singleton sets).

48
General Purpose Implementations
Hash Table Resizable array balanced tree linked list
Set HashSet TreeSet (sortedSet)
List ArrayList Vector LinkedList
Map HashMap Hashtable TreeMap (sortedMap)
49
Properties of the implementations
  • consistent names as well as consistent behavior.
  • fully implementations of all the optional
    operations.
  • All permit null elements, keys and values.
  • unsynchronized.
  • remedy the deficiency of Hashtable and Vector
  • can become synchronized through the
    synchronization wrappers
  • All have fail-fast iterators, which detect
    illegal concurrent modification during iteration
    and fail quickly and cleanly.
  • All are Serializable,
  • all support a public clone() method.
  • should be thinking about the interfaces rather
    than the implementations. The choice of
    implementation affects only performance.

50
HashSet vs treeSet (and HashMap vs TreeMap)
  • HashSet/ HashMap is much faster (constant time
    vs. log time for most operations), but offers no
    ordering guarantees.
  • always use HashSet/HashMap unless you need to use
    the operations in the SortedSet, or in-order
    iteration.
  • choose an appropriate initial capacity of your
    HashSet if iteration performance is important.
  • The default initial capacity is 101, and that's
    often more than you need.
  • can be specified using the int constructor.
  • Set s new HashSet(17) // set bucket size to 17

51
ArrayList vs LinkedList
  • Most of the time, you'll probably use ArrayList.
  • offers constant time positional access
  • Think of ArrayList as Vector without the
    synchronization overhead.
  • Use LikedList If you frequently add elements to
    the beginning of the List, or iterate over the
    List deleting elements from its interior.
  • These operations are constant time in a
    LinkedList but linear time in an ArrayList.

52
Wrapper Implementations
  • are implementations that delegate all of their
    real work to a specified collection, but add some
    extra functionality on top of what this
    collection offers.
  • These implementations are anonymous
  • the JDK provides a static factory method.
  • All are found in the Collections class which
    consists solely of static methods.
  • Synchronization Wrappers //K,V,E are method type
    parameters
  • public static CollectionltEgt synchronizedCollection
    (Collection ltEgt c)
  • public static SetltEgt synchronizedSet(SetltEgt s)
  • public static ListltEgt synchronizedList(List ltEgt
    list)
  • public static MapltK,Vgt synchronizedMap(MapltK,Vgt
    m)
  • public static SortedSetltEgt synchronizedSortedSet(S
    ortedSetltEgt s)
  • public static SortedMapltK,Vgt synchronizedSortedMap
    (SortedMap ltK,Vgtm)

53
read-only access to Collection/Maps
  • Unmodifiable Wrappers
  • public static CollectionltEgt unmodifiableCollection
    (CollectionltEgt c)
  • public static SetltEgt unmodifiableSet(SetltEgt s)
  • public static ListltEgt unmodifiableList(ListltEgt
    list)
  • public static MapltK,Vgt unmodifiableMap(MapltK,Vgt
    m)
  • public static SortedSetltEgt unmodifiableSortedSet(S
    ortedSetltEgt s)
  • public static SortedMapltK,Vgt unmodifiableSortedMap
    ( SortedMap ltK,Vgt m)

54
Convenience Implementations
  • mini-implementations that can be more convenient
    and more efficient then the general purpose
    implementations
  • available via static factory methods or exported
    constants in class Arrays or Collections.
  • List-view of an Array
  • List l Arrays.asList(new Object100) // list
    of 100 nulls.
  • Immutable Multiple-Copy List
  • List l new ArrayList(Collections.nCopies
    (1000, new Integer(1)))
  • Immutable Singleton Set
  • c.removeAll(Collections.singleton(e))
  • profession.values().removeAll(Collections.singleto
    n(LAWYER))
  • (Immutable) Empty Set and Empty List Constants
  • Collections.EMPTY_SET Collections.EMPTY_LIST
  • Collections.EMPTY_MAP // add(1) ?
    UnsupportedOp Exception

55
Convenience Implementations
  • (Immutable) Empty Set and Empty List methods
  • ltEgt SetltEgt emptySet()
  • ltEgt ListltEgt emptyList()
  • ltK,Vgt MapltK,Vgt emptyMap()

56
The Arrays class
  • static List asList(Object a) // a could not
    be of the type int,
  • Returns a fixed-size list backed by the specified
    array. cannot add/remove()
  • static int binarySearch(Type a, Type key)
  • Searches the specified array of bytes for the
    specified value using the binary search
    algorithm. Type can be any primitive type or
    Object.
  • static boolean equals(Type a1, Type a2)
  • static void fill(Type a ,int f, int t, Type
    val)
  • Assigns the specified val to each element of the
    specified array of Type.
  • static void sort(Type a , int f, int t)
  • Sorts the specified range of the specified array
    into ascending numerical order.
  • static sort(Object a, Comparator c ,int f,
    int t )
  • Sorts the specified array of objects according
    to the order induced by the specified
    comparator.

57
The Collections class
  • static int binarySearch(List list, Object key ,
    Comparator c)
  • static void copy(List dest, List src) //
    foreach i d.set(i, s.get(i))
  • static Enumeration enumeration(Collection c)
  • Returns an enumeration over the specified
    collection.
  • static void fill(List list, Object o)
  • static Object max(Collection coll , Comparator
    comp )
  • static Object min(Collection coll , Comparator
    comp )
  • static List nCopies(int n, Object o)
  • static void reverse(List l)
  • static Set singleton(Object o)
  • static Comparator reverseOrder() // assume
    a is of type String
  • // usage Arrays.sort(a, Collections.reverseOrde
    r())
  • static void shuffle(List list , Random rnd)
  • static void swap(List, int, int)
  • static void rotate(List, int d) // obj at i
    moved to ( i - d mod size())

58
The implementation of reverseOrder()
  • public static Comparator reverseOrder()
  • return REVERSE_ORDER
  • private static final Comparator REVERSE_ORDER
    new ReverseComparator()
  • private static class ReverseComparator
    implements Comparator,Serializable
  • public int compare(Object o1, Object o2)
  • Comparable c1 (Comparable)o1
  • Comparable c2 (Comparable)o2
  • return -c1.compareTo(c2)

59
The Collections class
  • static List singletonList(Object
    o)
  • static Map singletonMap(Object
    key, Object value)
  • static void sort(List list,
    Comparator c)
  • static Collection synchronizedCollection(Colle
    ction c)
  • static Collection unmodifiableCollection(Colle
    ction c)
  • static List EMPTY_LIST //The empty list
    (immutable).
  • static Map EMPTY_MAP //The empty map
    (immutable).
  • static Set EMPTY_SET //The empty set
    (immutable).
Write a Comment
User Comments (0)
About PowerShow.com