Download presentation
Presentation is loading. Please wait.
Published byRaymond Horn Modified over 9 years ago
1
1 The Map ADT © Rick Mercer
2
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
3
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 " )
4
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 accounts = new OrderedMap ();
5
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());
6
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));
7
7 What if the key exists? 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 ranking = new OrderedMap (); 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"));
8
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));
9
9 Generic Can have different types of keys and values — However, keys must implement Comparable because this Map has an ordering property OrderedMap ranking = new OrderedMap (); Recommendation: Use either String or Integer for the key type String implements Comparable
10
10 Which Data Structure? What data structures could we use to implement OrderedMap ? ________, __________, _________, __________ We will use a … see next slide
11
11 Code demo: OrderedMap Code demo: OrderedMap public class OrderedMap, 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; }
12
12 A picture of memory using the new TreeNode OrderedMap m = new OrderedMap (); 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
13
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
14
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
15
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
16
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 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.