COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
COMP 103 Linked Stack and Linked Queue.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Building Java Programs
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
COMP 103 Iterators and Iterable. RECAP  Maps and Queues TODAY  Queue Methods  Iterator and Iterable 2 RECAP-TODAY.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
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:
COMP T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
1 Concrete collections II. 2 HashSet hash codes are used to organize elements in the collections, calculated from the state of an object –hash codes are.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
Data structures Abstract data types Java classes for Data structures and ADTs.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Useful Data Structures in C++ and Java. Useful Data Structures in C++ (1) Templates are C++’s mechanism to define abstract objects which can be parameterized.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
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
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Java Collections Framework The client view. The Big Picture.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Building Java Programs
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Collections COMP 103 #3 2008T2.
QueueStack CS1020.
Some Collections: BAGS, SETS, and STACKS
Stacks and Queues.
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
Stacks and Queues.
COMP 103 Maps, Stacks Thomas Kuehne 2016-T2 Lecture 5
CS240: Advanced Programming Concepts
Stacks and Queues.
Building Java Programs
Stacks, Queues, and Deques
Stacks and Queues.
Java Collections Framework
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Stacks, Queues, and Deques
Stacks and Queues.
Presentation transcript:

COMP 103 Maps and Queues

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.

Maps 3 Key Value Not this kind of MAP Something that MAPs a key to a value NameAge Bob25 Rita34 Sam15 Adam 6 : : :

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

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

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 ?

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

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!

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

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

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

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

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

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