Title: CSECE 268
1Chapter 17 Maps
- CS/ECE 268
- Kristin Stechschulte
- Sophomore Computer Science
2Maps
- What is a Map?
- Comparison to Vectors/arrays.
- Everyday usages of Maps.
- The methods of a Map.
- The adapter Association.
- Running times of Maps with adapters.
- Pros v. Cons of Maps.
3Introduction
- Most of the data structures dealt with in the
class thus far have been ones that place emphasis
on the single independent values. - Bag, Stack, Queue, Deque, Etc.
- For example the Bag it holds a collection of
values and can tell whether a specific value is
part of the collection. - But sometimes is would be convenient to use a key
or a certain value to find what you are searching
for within a collection.
4What is a Map?
- A Map is a data abstraction which is specialized
for the specific purpose of making referencing
easier. - It may also be referred to as table, search
table, dictionary, associative array, or
associative container. - Elements of a Map are contained in pairs.
- Key a unique identifier that connects to one
associated entry (value). - Value an entry in the Map which is accessed by
a key.
5Map Information
- Two keys can point to the same information, but
Color A
Blue
Color B
6Map Information Continued
- A single key can not point to two separate values.
Blue
Color A
Purple
7Vector/array Comparison
- The Map, which is a keyed container, can be
contrasted with an indexed container, like a
Vector or an array. - In a Vector or array the indexed values are
associated to the entries, like a key in a Map. - But this has a set back, because there is no easy
way to remember which values are associated with
which indexes. - All the indexes in a Vector/array type are
restricted to the integer type.
8Vector/array Comparison
- Index Values Connected to the Index
0
For God so loved the world that He gave one and
only Son, that whosoever believes in Him shall
not perish but have eternal life.
The thief does not come except to steal, to kill,
and to destroy. I have come that they may have
life, and have it more abundantly.
1
For the wages of sin is death, but the gift of
God is eternal life in Christ Jesus our Lord!
2
God demonstrates His love towards us, in that
while we were yet sinners, Christ died for us.
3
Jesus said to him, I am the way, and the truth,
and the life no one comes to the Father but
through Me.
4
As many as received Him, to them He gave the
right to become children of God, even to those
who believe in His name.
5
By grace you have been saved through faith and
that not of yourselves, it is the gift of God
not as a result of works that no one should boast.
6
9Advantages of Maps
- With a key it is easier to remember the
connections, since it is not just an integer
value but it has a specific or easily
identifiable link to the value. - The Map is optimized for easy retrieval of values
when the key is input and known to the structure. - A Map does not limit the the number of elements
that can be held in the structure. -
10Map Examples
- There are many common example for the usages of
Maps as given over the past few days in class and
also presented in the book. - Dictionary key is the word, value is the
definition. - Encyclopedia key is the topic, value is the
information. - Address Book key is the name, value is the
address. - Bank Accounts key is the account number, value
is amount held in the savings or checking
account. - Student Accounts key is the student
identification number, value is the student name
and other info.
11More Map Examples
- Another great example is the Bible.
- The key are the Scripture references, and the
values are the Scripture verses.
12Map Methods
- The Map container implements the Map interface,
and has the properties associated with the normal
ADT. - int size()
- input - nothing
- output returns an integer value
- boolean isEmpty()
- input - nothing
- output returns a boolean value, true or false
13Map Methods continued
- There are also several methods that are unique to
the Map ADT. - void set(key, value)
- input a key object and a value object
associated with the key - output - nothing
- Object get(key)
- input a key object
- output the value object associated with the
key, if the key does not exist a
NoSuchElementException will be thrown - void removeKey(key)
- input a key object
- output it deletes the key and the value
associated with it, if the element does not exist
NoSuchElementException is thrown
14Map Methods continued
- boolean containsKey (key)
- input a key object
- output a boolean value (true or false) is
returned depending on whether the key object is
contained in the collection or not - Enumeration elements()
- input - nothing
- output an enumeration of the keys in the
collection, but the order is not specified or
defined
15Map Example...
16Map Implementation
- Because a Map is a ADT you cant use it directly.
- You must implement it through a sort of adapter
that is built on top of an underlying container. - The book points out how different effects can be
produced by changing the underlying data
structure. - By using a LinkedList, SortedVector, SortedList,
SkipList, AVLTree, OpenHashTable, HashTable, Etc. - The author uses a MapAdapter that manages
key/value pairs through the inner class named
Association.
17Association
- An Association holds a key and a value in data
fields that are like the class Object. - Comparisons between two associations are based
strictly on the keys and have nothing to do with
the values associated to the keys. - This is done by a Comparator object.
- Implementing MapAdapter from this class is
relatively simple. - The MapAdapter can also be built on top of any
other data structure that also supports the Bag
interface.
18Running Times
- The running time for the various operations of
the Map interface is dependent on the underlying
container.
19Running Times continued
- The simplest underlying container is the
LinkedList. - But the performance of all the functions in this
are slower, O(n), because it is linear. - The MapAdapter DOES NOT require ordering, but it
may help in the performance of the container when
you are searching for an object. - The AVLTree and the SkipList are considerably
faster than LinkedList, SortedList, or the
SortedVector, but the setback is that the
elements are required to be ordered if these
structures are used.
20Running Times continued
- The HashTable containers are overall the fastest,
and the elements are not required to be ordered.
But their speed is also dependent on the size of
the hash table and the load factor of the hash
table.
21Pros and Cons
- A Map is useful, because it reduces the work
needed to be done in performing a search over a
collection of keys and values. - It eliminates the need to iterate through the
collection while performing a search. - Able to find a Scripture by looking at the
reference, faster. - But there is a problem with it when you want to
go from the other direction and use the value to
find the key. - Cant type in the verse to find the reference
without going through each record step-by-step,
this takes a lot of time.
22To Wrap it up...
- A Map is an ADT that uses a pair of objects,
key/value, to store information. - It is similar to arrays and vectors, except that
it is easier to identify with the value by using
the key instead of being forced to use an index
number. - There are many everyday examples that use the map
structure - Dictionary, Student Account, and Bible, etc.
- To implement the Map structure an adaptor must be
used and it must be built on top of a data
structure - LinkedList, SortedVector, HashTable, etc.
23To Wrap it up...
- Performing different functions in the Map are
dependent on the running times of the underlying
containers. - The Map is a useful data structure when you want
and easy way to remember and search the values
that are stored.