Docsity

Duplication - Data Structures and Advanced Programming - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Duplication - Data Structures and Advanced Programming - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

CS61B Lecture

Today:

  • Asymptotic complexity (from last time)
  • Overview of standard Java Collections classes.
    • Iterators, ListIterators
    • Containers and maps in the abstract
    • Views

Readings for Today: Data Structures, Chapter 2.

Readings for next Topic: Data Structures, Chapter 3.

Some Intuition on Meaning of Growth

  • How big a problem can you solve in a given time?
  • In the following table, left column shows time in microseconds to

solve a given problem as a function of problem size N.

  • Entries show the size of problem that can be solved in a second, hour, month (31 days), and century, for various relationships be- tween time required and problem size.
  • N = problem size

Time (μsec) for Max N Possible in

problem size N 1 second 1 hour 1 month 1 century

lg N 10300000 101000000000 108 ·^10

11

109 ·^10

14

N 106 3. 6 · 109 2. 7 · 1012 3. 2 · 1015

N lg N 63000 1. 3 · 108 7. 4 · 1010 6. 9 · 1013

N 2 1000 60000 1. 6 · 106 5. 6 · 107

N 3 100 1500 14000 150000

2 N^20 32 41

Collection Structures in java.util

Collection

List Set

SortedSet

LinkedList ArrayList Vector HashSet TreeSet

Map Stack

SortedMap

HashMap WeakHashMap TreeMap

Key: interface

class

: extends : implements

The Collection Interface

  • Collection interface. Main functions promised:
    • Membership tests: contains (∈), containsAll (⊆)
    • Other queries: size, isEmpty
    • Retrieval: iterator, toArray
    • Optional modifiers: add, addAll, clear, remove, removeAll (set

difference), retainAll (intersect)

  • Design point (a side trip): Optional operations may throw

UnsupportedOperationException

  • An alternative design would have separate interfaces:

interface Collection { contains, containsAll, size, iterator, ... } interface Expandable { add, addAll } interface Shrinkable { remove, removeAll, difference, ... } interface ModifiableCollection extends Collection, Expandable, Shrinkable { } ... You’d soon have lots of interfaces. Perhaps that’s why they didn’t do it that way.)

Views

New Concept: A view is an alternative presentation of (interface to) an existing object.

  • For example, the sublist method is supposed to yield a “view of” part of an existing list:

L: at ax ban bat cat

︸ ︷︷ ︸

SL:

List L = new ArrayList(); L.add ("at"); L.add("ax"); ... List SL = L.sublist (1,4);

  • Example: after L.set(2, "bag"), value of SL.get(1) is "bag", and

after SL.set(1,"bad"), value of L.get(2) is "bad".

  • Example: after SL.clear(), L will contain only "at" and "cat".
  • Small challenge: “How do they do that?!”

Maps

  • A Map is a kind of “modifiable function:”

package java.util; public interface Map<Key,Value> { Value get (Object key); // Value at KEY. Object put (Key key, Value value); // Set get(KEY) -> VALUE ... }


Map<String,String> f = new TreeMap<String,String> (); f.put ("Paul", "George"); f.put ("George", "Martin"); f.put ("Dana", "John"); // Now f.get ("Paul").equals ("George") // f.get ("Dana").equals ("John") // f.get ("Tom") == null