Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "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"— Presentation transcript:

1 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 http://java.sun.com/doc s/books/tutorial/collecti ons/ You will be expected to: program to the generic Map and SortedMap interfaces by reading and using the API compare and contrast HashMap and TreeMap classes (benefits of using each, basic run time analysis)

2 2 Maps, Stacks and Queues KeysValues “Big Java”QA76.1 “Beginner's Cooking”L32.4 “War and Peace”H13.5 “Lost in Space”W33 MAP Map

3 3 Maps, Stacks and Queues The Map Interface A map structure is also known as a table or dictionary or association list. A map is a collection of pairs (key, value). The keys are unique within the map The map associates exactly one value with each key Maps form the basis for Databases Examples: map of student ids to student records map of words to frequency of occurrence in a document

4 4 Maps, Stacks and Queues The Map Interface (cont’) public interface Map { // Basic Operations int size(); boolean isEmpty(); boolean containsKey(Object key); boolean containsValue(Object value); V get(Object key); V remove(Object key); V put(K key, V value); // Bulk Operations void clear(); void putAll(Map m);

5 5 Maps, Stacks and Queues The Map Interface (cont’) // Collection Views Set keySet(); Collection values(); Set > entrySet(); // Interface for entrySet elements; defined inside Map interface Entry { K getKey(); V getValue(); V setValue(V value); }

6 6 Maps, Stacks and Queues Map Methods Basic methods: put adds a (key, value) entry; returns previous value for that key or null containsKey, containsValue check if a key or value is in the map get returns the value of a given key; returns null if key is not in the map remove removes the entry for that key; returns the value removed or null if the map doesn't contain the given key

7 7 Maps, Stacks and Queues Map Methods Collection Views: restructure the map (or parts of it) as a Collection so we can use iterators: keySet returns a Set containing all keys in the map values returns a Collection with all map values (may have duplicates) entrySet returns a Set of entries; each entry represents a (key, value) pair and it is defined by the interface Entry defined inside Map

8 8 Maps, Stacks and Queues Map Implementations The class HashMap provides an implementation of the Map interface. The underlying implementation is similar to a HashSet (it uses a hash table) When a (key, value) pair is added to a HashMap, the hash code is generated using only the key (not the value) Assuming a good hashCode() method for the Key, the put, get and remove methods run in O(1) time.

9 9 Maps, Stacks and Queues Map Examples A function that returns a map of words to the number of times each word occurs in a text document (doc): public static Map frequencies(Collection doc) { Map map = new HashMap (); for (String word : doc) { if ( __________________________________ ) ______________________________________ else ______________________________________ } return map; }

10 10 Maps, Stacks and Queues Map Examples A generic method that prints out all the key-value pairs of a Map : public static void printMap(Map theMap) { for(Map.Entry e : theMap.entrySet()) { System.out.println(e.getKey() + ": " + e.getValue()); } } Exercise: Write the same function using an iterator instead of a for-each loop.

11 11 Maps, Stacks and Queues Sorted Map The SortedMap interface extends the Map interface. A SortedMap maintains the entries in the map sorted by their key (not their value) The class TreeMap implements the SortedMap interface TreeMap uses a similar underlying implementation to TreeSet. The get, put and remove operations run in O( log N ) time.

12 12 Maps, Stacks and Queues SortedMap Interface public interface SortedMap extends Map { // Range-view SortedMap subMap(K fromKey, K almostToKey); SortedMap headMap(K almostToKey); SortedMap tailMap(K fromKey); // Endpoints K firstKey(); K lastKey();... }

13 13 Maps, Stacks and Queues Midterm (slide 1 of 2) Class Design, lecture 1 Class Contracts, lecture 2 Exeptions, lecture 3 Not "checked versus unchecked" Not "finally clause" equals(), lecture 4 Unit Testing, lecture 5 including selecting test cases using equivalence partitioning Class Design II, lecture 6 including coupling and cohesion Don't need to memorize UML notation, but you need to know how to read it and use it Advanced Inheritance, lecture 7 especially the substitution principle

14 14 Maps, Stacks and Queues Midterm (slide 2 of 2) Collections, Iterators, Lists, Sets, Maps, lectures 9, 10, 12, 13, 14 Don't need to memorize the APIs but know how to read them and use them Don't need to memorize the suggested hashCode algorithm Don't need to know Stacks, Queues, Dequeue Generic classes and generic methods, lectures 9, 10, 12, 13, 14 know how read them and write them Time complexity, lecture 11 especially comparing data structures given their Big-O complexity


Download ppt "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"

Similar presentations


Ads by Google