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.

Slides:



Advertisements
Similar presentations
1 VBScript Session What we learn last session? Regulars Expressions. Methods and properties. How to use the object and his collections. How to create.
Advertisements

Why not just use Arrays? Java ArrayLists.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
© 2004 Goodrich, Tamassia Hash Tables1  
1 Dictionary Often want to insert records, delete records, search for records. Required concepts: Search key: Describe what we are looking for Key comparison.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
hashing1 Hashing It’s not just for breakfast anymore!
Hash Tables1 Part E Hash Tables  
Abstract Data Types Linked Lists. Abstract Data Type(ADT) 4 n ADT--a specification, in abstract terms only, without reference to programming language.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Java's Collection Framework
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
(c) University of Washington14-1 CSC 143 Java Collections.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
1 Sets and Maps Starring: keySet Co-Starring: Collections.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
ICOM 4035 – Data Structures Lecture 11 – Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Hash Tables1   © 2010 Goodrich, Tamassia.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Hash Tables1  
The Map ADT and Hash Tables. 2 The Map ADT  Map: An abstract data type where a value is "mapped" to a unique key  Need a key and a value to insert new.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
1 Interfaces in Java’s Collection Framework Rick Mercer.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Tries Data Structure. Tries  Trie is a special structure to represent sets of character strings.  Can also be used to represent data types that are.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Maps Nick Mouriski.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
CSE 143 Lecture 11: Sets and Maps reading:
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
Implementing the Map ADT.  The Map ADT  Implementation with Java Generics  A Hash Function  translation of a string key into an integer  Consider.
Click to edit Master text styles Stacks Data Structure.
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Data Structures and Analysis (COMP 410)
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Rick Mercer, Allison Obourn, Marty Stepp
Chapter 10: An Array Instance Variable
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 7 The Java Array Object © Rick Mercer.
CSE 373 Separate chaining; hash codes; hash maps
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Presentation transcript:

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 a key and its value  Only need the key to get the mapped value and remove the key / value mapping

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 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 Put and get  OrderedMap will be a collection class  Add mappings, keys and values with put accounts.put("M", new BankAccount("Chris", )); accounts.put("G", new BankAccount("Kim", ));  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 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", ));

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 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 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 Which Data Structure?  What data structures could we use to implement OrderedMap ? ________, __________, _________, __________  We will use a … see next slide

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