Save
...
Finals
Data Structures
Map
Save
Share
Learn
Content
Leaderboard
Learn
Created by
michael
Visit profile
Cards (19)
The
Map Interface
• It deals with collections of ordered pairs.
Think of the pair as consisting of a
key K
(to search for) and an associated
value V.
For example, the
key
might be a student ID number and name
The Map interface takes a base type for the
key
and a base type for the
value.
Use the
put
method to add a key/value pair to the collection and the
get
method to retrieve the value for a given key.
The base types must be
objects
and cannot be
primitive
data types.
public Base_Type_Value put(Base_Type_Key k, Base_Type_Value v)
• Associates the
value
v with the
key
k.
public Base_Type_Value
put
(Base_Type_Key k, Base_Type_Value v)
Returns the
previous value
for
k
or null if there was no previous mapping.
public Base_Type_Value get(Object k)
• Returns the
value
mapped to the
key
k or null if no mapping exists.
public Base_Type_Value
remove
(Object k)
• Removes the mapping of
key
k from the
map
if present.
public Base_Type_Value remove(Object k)
Returns the previous
value
for the key k or
null
if there was no previous mapping.
public boolean
containsKey
(Object k)
• Returns
true
if the key k is a
key
in the map.
public boolean
containsValue
(Object v)
• Returns true if the
value
v is a
value
in the map.
public boolean hashCode()
• Returns the
hash code
value for the calling object.
public boolean isEmpty()
• Returns true if the map contains no mappings.
public Set <Base_Type_Key>
keySet
()
• Returns a
set
containing all of the keys in the map.
public
Collection
<Base_Type_Values> values()
• Returns a collection containing all of the
values
in the map.
The
HashMap
class implements the Map interface and is used to store a map from a key object to a value object.
class HashMap uses the
hash table
algorithm.
HashMap is able to quickly
retrieve
the value object when given the
key
object.