Maps - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Maps

Description:

Chocolate - chocolate chip cookies. Coconut - coconut macaroons. Recipe list. Chocolate chip cookies - butter, baking soda, chocolate ... etc. ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 19
Provided by: cosc3
Category:

less

Transcript and Presenter's Notes

Title: Maps


1
Maps
Chapter 17
Presentation by Adam Smith
2
Index
MAPS
-Definitons -Use of maps -Characteristics -Meth
ods -Usfulness -How to set up -Problems
with -Bibliography
3
Definitions
Map is a data abstraction consisting of a key
and one value associated with that
key. This means that you cant have two keys of
the same name.
MultiMap is a data abstraction consisting of a
key and one or more values associated
with that key
Also Known As
Table Search Table Dictionary Associative
Array Associative container
4
Why use a map ?
  • easy to understand
  • -good way to keep information less confusing
  • Example-vectors can be used but are very hard to
  • Keep the information straight

Problems with this
v.addElement(bob) v.addElement(93.2)
v.addElement(tom) v.addElement(90.2)  String
input inputbox.getText( ) Enumeration enum
v.elements( )   while (enum.hasMoreElements( ))
String s (String)enum.nextElement(
) if(input s) float avg
(float)enum.nextElement( ) else
float avg (float)enum.nextElement(
)
What if
v.addElement(93.2) v.addElement(bob) v.addElem
ent(90.2) v.addElement(tom)
5
Characteristics
Map
- Each key identifies one entry
- Each key must be unique
-Although each key must be unique the values
associated with the keys do not have to be
unique
MultiMap
- Each key can identify one or more entries
- Each key must be unique
-Although each key must be unique the values
associated with the keys do not have to be
unique
6
Characteristics cont.
-When map is described, Multimap is envisioned
key
United states
values
Arizona
..
Ohio
..
Wyoming
Canada
Countries
.
..
Ontario
Quebec
.
Spain
7
Methods
Map
-Void set(key,value)
-Object get(key)
-void removeKey(key)
-boolean isEmpty()
-int size()
-boolean containsKey(key)
-Enumeration elements()
Multimap - same except now a linked list is
implemented for the objects
8
JDS Methods in detail
Map
Void set(key,value) -Assumes that the objects go
together -OVERRIGHTS any previous associations
that used the same key -Returns
nothing Object get(key) -Assumes the key is
comparable with keys already there -Throws a
NOSuchElementException - Returns Object
value void removeKey(key) -Assumes the key is
comparable with the keys already there -Throws a
NOSuchElementException -Returns nothing
9
JDS Methods in detail Cont.
int size() -Returns The size of the collection
(The number of keys) -NO Change will occur to
the current collection boolean
containsKey(key) -Returns 1 if the element is in
the collection 0 if the element is not in
the collection Enumeration elements() -Returns
an enumeration of keys being held in the
collection -The order in which the elements
appear is undefined
10
JDS Methods in detail Cont.
Multimaps
All methods are the same except for set() and
get()
Void set(key,value) -Assumes that the objects go
together -if the same key is being used it adds
the new value to previous associations that
used the same key -Returns nothing Object
get(key) -Assumes the key is comparable with
keys already there -Throws a NOSuchElementExcepti
on -Returns A linked list of the objects
11
JDS Methods in detail Cont.
public Object get (Object key) LinkedList e
(LinkedList) data.get(key) return
e.elements()   public void removeKey (Object
key) data.removeKey(key)
  public void set (Object key, Object value)
if (data.containsKey(key)) LinkedList
ldata (LinkedList)
data.get(key) ldata.addElement(value)
else LinkedList ldata new
LinkedList() ldata.addElement(value) data.
set(key, ldata)
12
Usefulness
Associations
Map
Multimap (of things you want for lunch)
key
Sandwich
value
13
Usefulness cont.
Multimap
Party planning
Sickness
14
Usefulness (Multimap)
  • -What if we want to maintain a database of
    recipes that use a
  • specific ingredient
  • Could have each ingredient serve as a key in a
    multimap.
  • -Then we could store a list of all the recipes
    in another list
  • Ex.) Recipe/ingredient database program

Ingredient list Butter -gt chocolate chip
cookies, coconut macaroons, oatmeal-date
bars Baking soda -gt chocolate chip cookies,
oatmeal-date bars Baking powder -gt coconut
macaroons Chocolate -gt chocolate chip
cookies Coconut -gt coconut macaroons
Recipe list Chocolate chip cookies -gt butter,
baking soda, chocolate etc.
15
How would we set up a multimap?
Multimap looks like
Insertion
Multimap party new Multimap() Party.set(Beer,Co
ors) Party.set(Beer,Heineken) Party.set(Beer,Bud
light) Party.set(Beer,Sam Adams )
Party
Extraction
LinkedList stuff new LinkedList()
stuff (LinkedList)party.get(Beer)
Linked list looks like this
head
heineken
Sam Adams
Beer
coors
Bud light
16
  • Problems with maps
  • Searching by values is time consuming
  • -you must go through each key and check its
    contents
  • - even after the correct value is found the
    search must
  • continue, because while the keys have to be
    unique
  • the values do not.

Possible Solution -use more than one type of map
to store the same data -Like in the recipie
problem shown earlier
17
One Last Example
The address book JDS application
18
BIBLIOGRAPHY -Classical Data Structures in
Java by Timothy Budd
Write a Comment
User Comments (0)
About PowerShow.com