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

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
Using Maps. A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable,
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
Professor Evan Korth (adapted from Sun’s collections documentation)
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Using Maps. 2 A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable,
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
CS Collection and Input/Output Classes CS 3331 Fall 2009.
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.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
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.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
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.
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
The Java Collections Framework Based on
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Java 2 Collections Bartosz Walter Software Engineering II.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
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.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
Set and Map IS 313, Skeletons  Useful coding patterns  Should understand how and why they work how to use  possible quiz material.
Maps Nick Mouriski.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
Collections Dwight Deugo Nesa Matic
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:
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
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.
Java Collections OOP tirgul No
Road Map CS Concepts Data Structures Java Language Java Collections
Part of the Collections Framework
Collections in Java The objectives of this lecture are:
Hashing in java.util
Set and Map IS 313,
Presentation transcript:

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