Download presentation
Presentation is loading. Please wait.
Published byMorris James Modified over 8 years ago
1
COMP 103 Maps and Queues
2
RECAP Iterators (for-each loop) Bag, Sets, and Stacks - a class, not interface TODAY Maps and Queues 2 RECAP-TODAY QUICK TIP: Comment your assignment code for extra points & remember to fill in your details (name etc) at the top of the file.
3
Maps 3 Key Value Not this kind of MAP Something that MAPs a key to a value NameAge Bob25 Rita34 Sam15 Adam 6 : : :
4
Maps Collection of data, but not of single values: Map = a set of pairs of keys and values Constrained access: get values via keys. No duplicate keys Lots of implementations, most common is HashMap. “Sharon” ⇒ 442 “Marcus” ⇒ 427 “Rashina” ⇒ 426 “John” ⇒ 337 “Pondy” ⇒ 429 get (“Rashina”) put (“Marcus”, 443) put (“Sharon”, 336) 4
5
Maps When declaring and constructing, must specify two types: Type of the key, and type of the value private Map phoneBook; : phoneBook = new HashMap (); Central operations: get(key) → returns value associated with key (or null) put(key, value) → sets the value associated with key (and returns the old value, if any) remove(key) → removes the key and associated value (and returns the old value, if any) containsKey(key) → boolean size() 5
6
Iterating through a Map How do you iterate through a Map ? (eg, to print it out) A Map isn’t just a collection of single items! ⇒ could iterate through the collection of keys ⇒ could iterate through the collection of values ⇒ could iterate through the collection of key-value pairs Java Map allows all three! keySet()→ Set of all keys for (String name : phonebook.keySet()){…. values()→ Collection of all values for (Integer num : phonebook.values()){…. entrySet()→ Set of all Map.Entry’s for (Map.Entry entry : phonebook.entrySet()) { … entry.getKey() … … entry.getValue()… 6 Why Collection of values not Set of values like the other two ?
7
Example of using Map Find the highest frequency word in a file ⇒ must count frequency of every word. ie, need to associate a count (int) with each word (String) ⇒ use a Map of “word–count” pairs: Two Steps: construct the counts of each word: countWords(file) → map find the highest countfindMaxCount(map) → word UI.println( findMaxCount( countWords(file) ) ); 7
8
Example of using Map (similar to Assign#2) /** Construct histogram of counts of all words in a file */ public Map countWords(Scanner sc){ // construct new map // for each word in file // if word is in the map, increment its count // else, put it in map with a count of 1 // return map } /** Find word in histogram with highest count */ public String findMaxCount(Map counts){ // for each word in map // if has higher count than current max, record it // return current max word } 8 Design the step-by-step solution (algorithm) before you code!
9
Example of using Map /** Construct histogram of counts of all words in a file */ public Map countWords(Scanner scan){ Map counts = new HashMap (); while (scan.hasNext()){ String word = scan.next(); if ( counts.containsKey(word) ) counts.put(word, counts.get(word)+1); else counts.put(word, 1); } return counts; } /** Find word in histogram with highest count */ public String findMaxCount(Map counts){ // for each word in map // if has higher count than current max, record it // return current max word } 9
10
Iterating through Map: entrySet public String findMaxCount(Map counts){ String maxWord = null; int maxCount = -1; for (Map.Entry entry : counts.entrySet() ){ if (entry.getValue() > maxCount){ maxCount = entry.getValue(); maxWord = entry.getKey(); } return maxWord; } “public” ⇒ 1 “Map” ⇒ 2 “String” ⇒ 5 “counts” ⇒ 2 “findMaxCount” ⇒ 1 Map.Entry - getKey() - getValue() 10
11
Queues Queues are like Stacks, but First In First Out principle (FIFO) Collection of values with an order Constrained access: Only remove from the front Two varieties: Ordinary queues: only add at the back Priority queues: add with a given priority 11
12
Queues Used for Operating Systems, Network Applications, multi-user systems Handling requests/events/jobs that must be done in order (often called a “buffer” in this context) Simulation programs Representing queues in the real world (traffic, customers, deliveries, ….) Managing the events that must happen in the future Search Algorithms Computer Games Artificial Intelligence Java provides a Queue interface several classes (ie. implementations) : LinkedList, PriorityQueue, …. 12
13
Queue Operations offer(value) ⇒ boolean add a value to the queue (sometimes called “enqueue”, like “push” ) poll() ⇒ value remove and return value at front/head of queue or null if the queue is empty (sometimes called “dequeue”, like “pop”) peek() ⇒ value return value at head of queue, or null if queue is empty (doesn’t remove from queue) remove() and element() like poll() and peek(), but throw exception if queue is empty. Why use Queue (or Stack), instead of List ?? 13
14
Summary Map key-value pairs 3 ways to iterate: over set of keys, over collection of values, over set of key-value pairs Key-value pairs represented using Map.Entry Queue First in, first out Always remove from one end An interface, concrete class implementations: linkedlist, priority queue Ordinary queue: add end other end Priority queue: add anywhere with priority 14
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.