CSE 12 The Map Abstract Data Type
CSE 12 The Map Abstract Data Type
17
Map()
pre-condition:
responsibilities:
post-condition:
returns:
Map Operations
none
constructorcreate an empty map
size is set to 0
nothing
Map Operations
get( KeyType key )
pre-condition:
key is not null and can be compared for equality to other
keys in this map
responsibilities:
gets the value associated with key in this map
post-condition:
the map is unchanged
returns:
null if key was not found in this map, the value associated
with key otherwise
exception:
if key is null or cannot be compared for equality to other
keys in this map
remove( KeyType key )
pre-condition:
key is not null and can be compared for equality to other keys in
this map
responsibilities:
remove the value from this map associated with key
post-condition:
size is decreased by one if key was found in this map
returns:
null if key was not found in this map, the value associated with
key otherwise
exception:
if key is null or cannot be compared for equality to other keys in
this map
6
Map Operations
containsValue( ValueType value )
pre-condition:
value is not null and can be compared for equality to other
values in this map
responsibilities:
determines if this map contains an entry containing value
post-condition:
the map is unchanged
returns:
true if value was found in this map, false otherwise
exception:
if value is null or cannot be compared for equality to other
values in this map
containsKey( KeyType key )
pre-condition:
key is not null and can be compared to other keys in this map
responsibilities:
determines if this map contains an entry with the given key
post-condition:
the map is unchanged
returns:
true if key was found in this map, false otherwise
exception:
if key is null or cannot be compared for equality to other keys in
this map
Map Operations
values()
pre-condition:
responsibilities:
post-condition:
returns:
none
provides a Collection view of all the values contained in
this map. The returned Collection supports element removal, but
not element addition. A removal made to the Collection
is reflected in this map and vice versa
the map is unchanged
a Collection providing a view of the values from this map
But now put() in a linked list of length n takes time O(n), and
implementing get() also takes time O(n) in the worst case
We would like to do better than that! Consider using a hash
table to implement Map
10
11
13
The Bad: To guarantee a unique bucket for each key, the table
would have to have as many buckets as there are possible keys
Since the set of possible keys may be quite large (think of
English words, or people's names, or 32-bit integers, etc.), this
is not practical in general:
The table would be huge, and (since the number of actual keys
stored in the table is typically much smaller than the number of
possible keys) would waste a lot of space
14
16
17
Hashing examples
In the following examples, keys are Strings, and the hash
function hash(String s) is defined as:
18
19
(d) Inserting
Carol then
Dora grows the
cluster
clustering keys occupying adjacent locations in the table. During an insert, a key that hashes to
any location in a cluster will grow the cluster; the cluster acts like a linked list
20
21
23
24
Next time
Hash table time costs
Hash functions
The Map<K,V> interface and implementations
Reading: : Gray, Ch 12
25