Maps You will be expected to program to
Maps You will be expected to: • program to the generic Map and Sorted. Map interfaces by reading and using the API • compare and contrast Hash. Map and Tree. Map classes (benefits of using each, basic run time analysis) Maps, Stacks and Queues 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/ 1
MAP Maps, Stacks and Queues Keys Values “Big Java” QA 76. 1 “Beginner's Cooking” L 32. 4 “War and Peace” H 13. 5 “Lost in Space” W 33 2
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 Maps, Stacks and Queues 3
The Map Interface (cont’) public interface Map<K, V> { // Basic Operations int size(); boolean is. Empty(); boolean contains. Key(Object key); boolean contains. Value(Object value); V V V get(Object key); remove(Object key); put(K key, V value); // Bulk Operations void clear(); void put. All(Map<? extends K, ? extends V> m); Maps, Stacks and Queues 4
The Map Interface (cont’) // Collection Views Set<K> key. Set(); Collection<V> values(); Set<Map. Entry<K, V>> entry. Set(); // Interface for entry. Set elements; defined inside Map interface Entry<K, V> { K get. Key(); V get. Value(); V set. Value(V value); } } Maps, Stacks and Queues 5
Map Methods • Basic methods: • put adds a (key, value) entry; returns previous value for that key or null • contains. Key, contains. Value 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 • removes the entry for that key; returns the value removed or null if the map doesn't contain the given key Maps, Stacks and Queues 6
Map Methods • Collection Views: restructure the map (or parts of it) as a Collection so we can use iterators: • key. Set returns a Set containing all keys in the map • values returns a Collection with all map values (may have duplicates) • entry. Set returns a Set of entries; each entry represents a (key, value) pair and it is defined by the interface Entry defined inside Maps, Stacks and Queues 7
Map Implementations • The class Hash. Map provides an implementation of the Map interface. • The underlying implementation is similar to a Hash. Set (it uses a hash table) • When a (key, value) pair is added to a Hash. Map, the hash code is generated using only the key (not the value) • Assuming a good hash. Code() method for the Key, the put, get and remove methods run in O(1) time. Maps, Stacks and Queues 8
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<String, Integer> frequencies(Collection<String> doc) { Map<String, Integer> map = new Hash. Map<String, Integer>(); for (String word : doc) { if ( _________________ ) ___________________ else ___________________ } } return map; Maps, Stacks and Queues 9
Map Examples • A generic method that prints out all the key-value pairs of a Map: public static <K, V> void print. Map(Map<K, V> the. Map) { for(Map. Entry<K, V> e : the. Map. entry. Set()) { System. out. println(e. get. Key() + ": " + e. get. Value()); } } • Exercise: Write the same function using an iterator instead of a for-each loop. Maps, Stacks and Queues 10
Sorted Map • The Sorted. Map interface extends the Map interface. • A Sorted. Map maintains the entries in the map sorted by their key (not their value) • The class Tree. Map implements the Sorted. Map interface • Tree. Map uses a similar underlying implementation to Tree. Set. The get, put and remove operations run in O( log N ) time. Maps, Stacks and Queues 11
Sorted. Map Interface public interface Sorted. Map<K, V> extends Map<K, V> { // Range-view Sorted. Map<K, V> sub. Map(K from. Key, K almost. To. Key); Sorted. Map<K, V> head. Map(K almost. To. Key); Sorted. Map<K, V> tail. Map(K from. Key); // Endpoints K first. Key(); K last. Key(); . . . } Maps, Stacks and Queues 12
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 Maps, Stacks and Queues 13
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 hash. Code 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 Maps, Stacks and Queues 14
- Slides: 14