20-OrderedMapAsBST

advertisement
The Map ADT
© Rick Mercer
1
The Map ADT
 A Map is an abstract data type where a value is
"mapped" to a unique key
 Also known as Dictionary
 Need a key and its value
 Only need the key to get the mapped value and
remove the key / value mapping
2
The Map ADT


Traditional method names to add and retrieve:
put and get
Need a key and a value (you here) to add to
collection
put("yourStudentID", you)

Use a key to get the value mapped to the key
get("studentID")
3
Key and Value
 With Java generics, you need to specify
—
—
the type of the value and
the type of the key
 In the following examples, we'll use two
type arguments, the key type is String and
the value type is BankAccount
OrderedMap<String, BankAccount> accounts
= new OrderedMap<String,
BankAccount>();
4
Put and get
 OrderedMap will be a collection class
 Add mappings, keys and values with put
accounts.put("M", new BankAccount("Chris", 111.11));
accounts.put("G", new BankAccount("Kim", 222.22));
 Retrieve values mapped to keys with get:
BankAccount current = accounts.get("M");
assertEquals(111.11, current.getBalance(), 1e-8);
assertEquals("Chris", current.getID());
// Get a different value
current = accounts.get("G");
assertEquals(222.22, current.getBalance(), 1e-8);
assertEquals("Kim", current.getID());
5
Returning null
 get returns null if the key is not mapped to a value
assertNull(accounts.get("Not in the map"));
 put returns null if the key is not in the Map
assertNull(accounts.put("ThirdKey",
new BankAccount("Third", 333.333));
6
What if the key exists?
 If a key exists, put returns the previous value mapped
to key
 This can actually be useful
 If worried, use if(ranking.containsKey(1));
// Use different types for key and value
OrderedMap<Integer, String> ranking
= new OrderedMap<Integer, String>();
assertNull(ranking.put(1, "Kim"));
assertNull(ranking.put(2, "Li"));
// “Third” replaces “Kim” as the value mapped to 1
assertEquals("Kim", ranking.put(1, "Third"));
7
remove
 remove returns null if key is not found
—
or returns the value associated with the key if the
mapping (the key-value pair) was successfully
removed from the collection
// Key 2 exists
assertEquals("Li", ranking.remove(2));
// Key 2 no longer exists, remove returns null
assertNull(ranking.remove(2));
8
Generic
 Can have different types of keys and values
—
However, keys must implement Comparable
because this Map has an ordering property
OrderedMap<String, ValueType> ranking = new
OrderedMap<String, ValueType>();
String
implements
Comparable
 Recommendation: Use either String or
Integer for the key type
9
Which Data Structure?
 What data structures could we use to implement
OrderedMap<K, V>?
________ , __________ , _________ , __________
 We will use a …
see next slide
10
Code demo: OrderedMap<K, V>
public class OrderedMap<K extends Comparable<K>, V> {
private class MapNode {
private K key;
private V value;
private MapNode left;
private MapNode right;
public MapNode(K theKey, V theValue) {
key = theKey;
value = theValue;
left = null;
right = null;
}
} // end class MapNode
private MapNode root;
public OrderedMap() { // Create an empty OrderedMap
root = null;
}
11
A picture of memory using the new
TreeNode
OrderedMap<String, BankAccount> m =
new OrderedMap<String, BankAccount>();
m.put("M", new BankAccount("Li", 1.00));
m.put("G", new BankAccount("Cy", 2.00));
m.put("S", new BankAccount("Jo", 3.00));
"M"
"G" "Cy" 2.0
root
"Li" 1.0
"S"
"Jo" 3.0
12
m.get("Q");
While there are more nodes to consider {
if key equals MapNode's key, return value
else if key < MapNode's key, go left
else if key > MapNode's key, go right
}
return null
root
"M"
"Li" 1.0
"G" "Cy" 2.0
ref
"S"
"Q"
"Al" 3.0
"Jo" 3.0
"V"
"Ky" 3.0
13
m.get("Q");
While there are more nodes to consider {
if key equals MapNode's key, return value
else if key < MapNode's key, go left
else if key > MapNode's key, go right
}
return null
root
"M"
"Li" 1.0
"G" "Cy" 2.0
ref
"S"
"Q"
"Al" 3.0
"Jo" 3.0
"V"
"Ky" 3.0
14
m.get("Q");
While there are more nodes to consider {
if key equals MapNode's key, return value
else if key < MapNode's key, go left
else if key > MapNode's key, go right
}
return null
object reference on right
root
"M"
"Li" 1.0
"G" "Cy" 2.0
ref
"S"
"Q"
"Al" 3.0
"Jo" 3.0
"V"
"Ky" 3.0
15
Map methods needed
public V put(K key, V value)
• Associates key to value and stores mapping.
• Return null if the key does not exist
• If the key exists, replace the value with a new value
and return the value that is gone
public int size()
• Return the number of mappings
public V get(K key)
• Return the value to which key is mapped or null if the
key does not exist
public boolean containsKey(K key)
returns true if the Map contains this key
16
Download