1 the map adt © rick mercer. 2 the map adt a map is an abstract data type where a value is...

16
1 The Map ADT © Rick Mercer

Upload: raymond-horn

Post on 29-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

1

The Map ADT

© Rick Mercer

Page 2: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

2

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

Page 3: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

3

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")

Page 4: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

4

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>();

Page 5: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

5

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());

Page 6: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

6

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));

Page 7: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

7

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"));

Page 8: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

8

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));

Page 9: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

9

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>();

Recommendation: Use either String or Integer for the key type

String implements Comparabl

e

Page 10: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

10

Which Data Structure?

What data structures could we use to implement OrderedMap<K, V>?

________ , __________ , _________ , __________

We will use a … see next slide

Page 11: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

11

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; }

Page 12: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

12

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" "Li" 1.0

"S" "Jo" 3.0"G" "Cy" 2.0

root

Page 13: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

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

"M" "Li" 1.0

"S" "Jo" 3.0 "G" "Cy" 2.0

root

"Q" "Al" 3.0 "V" "Ky" 3.0

ref

Page 14: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

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

"M" "Li" 1.0

"S" "Jo" 3.0 "G" "Cy" 2.0

root

"Q" "Al" 3.0 "V" "Ky" 3.0

ref

Page 15: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

15

m.get("Q"); While there are more nodes to consider { if key equals MapNode's key, return value object reference on right

else if key < MapNode's key, go left else if key > MapNode's key, go right

}return null

"M" "Li" 1.0

"S" "Jo" 3.0 "G" "Cy" 2.0

root

"Q" "Al" 3.0 "V" "Ky" 3.0

ref

Page 16: 1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need

16

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 gonepublic 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 existpublic boolean containsKey(K key)

returns true if the Map contains this key