Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Why not just use Arrays? Java ArrayLists.
Stacks, Queues, and Linked Lists
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 Tables and Priority Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring.
hashing1 Hashing It’s not just for breakfast anymore!
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1 Topic 9 Maps "He's off the map!" -Stan (Mark Ruffalo) Eternal Sunshine of the Spotless Mind.
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.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary A Java Interface Iterators Using the ADT Dictionary A Directory of Telephone.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
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.
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Hashing CS 105. Hashing Slide 2 Hashing - Introduction In a dictionary, if it can be arranged such that the key is also the index to the array that stores.
1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
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.
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.
2-1 Week 2 Sets Set concepts (you should know these!) Set applications. A set ADT (abstract data type): requirements, contract. Implementations of sets:
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
Data structures Abstract data types Java classes for Data structures and ADTs.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
1 Interfaces in Java’s Collection Framework Rick Mercer.
Hashing CS 110: Data Structures and Algorithms First Semester,
9-1 9 Set ADTs Set concepts. Set applications. A set ADT: requirements, contract. Implementations of sets: using arrays, linked lists, boolean arrays.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
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 Dwight Deugo Nesa Matic
CSE 143 Lecture 11: Sets and Maps reading:
Hashing By Emily Nelson. The Official Definition Using a hash function to turn some kind of data in relatively small integers or Strings The “hash code”
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Maps Rem Collier Room A1.02 School of Computer Science and Informatics
11 Map ADTs Map concepts. Map applications.
Efficiency of in Binary Trees
Part of the Collections Framework
Maps "He's off the map!" -Stan (Mark Ruffalo) Eternal Sunshine of the Spotless Mind.
"He's off the map!" - Eternal Sunshine of the Spotless Mind
CS2110: Software Development Methods
ArrayLists 22-Feb-19.
L5. Necessary Java Programming Techniques
Hashing in java.util
Collision Handling Collisions occur when different elements are mapped to the same cell.
Web Design & Development Lecture 6
Presentation transcript:

Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.

Searching The majority of data structures we will study in this course are for the purpose of searching. –Involves storing information about a piece of data in the data structure –Must be able to retrieve the information later There are two major applications for searching: Sets and Maps

Sets and Maps Sets –The simplest application of searching –Place an item into a data structure –Ask the question about an item, “is it in the data structure?” Maps –Store a pair, a key and information about the key in a data structure –Ask if a key is in the data structure (basically, a set) –Retrieve information stored about a key

Examples Set Example: Dictionary for a spell checker –Want to ask whether a word is in the dictionary –Want to add a word to the dictionary Map Example: Students’ names and addresses –Given a student’s name, want to be able to get his/her address –Want to add a new student with address –Want to remove a student (for drop-out or xfer) –Want to change a student’s address –Want to see if a student is enrolled

Operations on Set ADT void clear() - Empty the set. boolean add(Object obj) - Add to the set, returns whether it is a new item boolean contains(Object obj) - In the set? boolean isEmpty() - Nothing in the set? boolean remove(Object obj) - Remove from the set. Returns true if it was in the set. int size() - Number of items in the set. Iterator iterator() - Iterate over all set members Note that Sets are templated. Can follow them with the type of item they stored in angle brackets (<>).

Operations on Map ADT clear, isEmpty, and size operations as in set. Object put(Object key, Object value) - Add key/value mapping. Return previous value mapped to key or null if none. Object get(Object key) - Return value key maps to. Object remove(Object key) - Remove object from map. Return mapped value (or null if none). boolean containsKey(Object key) - Key in map? Iterator Like Sets, Maps are templated. Can follow them with a pair of types in angle brackets.

Equals In order to retrieve an item, must have the concept of when two items are equal –E.g. is “George W. Bush” the same item as “George H. W. Bush” –Is “USA” the same as “U.S.A.”? Maps and sets use the equals() method –Defined for any kind of object –Can specialize it if necessary When adding an item to a set or map, checks if it is equal to an item already there. –If so, replace (map) or do nothing (set) –If not, add the new member/mapping.

Set Example: States bordering MD Set tomd=new Set (); boolean b; b=tomd.add(“VA”); b=tomd.add(“WV”); b=tomd.add(“PA”); b=tomd.add(“DE”) b=tomd.add(“OH”); print(tomd.contains(“VA”)); //True print(tomd.contains(“NJ”)); //False tomd.remove(“OH”);//OOPS! Not Ohio print(tomd.size());//4 tomd.clear(); print(tomd.size());//0

Map Example: State Capitals Map caps=new Map (); Object ob; ob=caps.put(“MD”,”Annapolis”); ob=caps.put(“PA”,”Harrisburg”); ob=caps.put(“NY”,”New York City”); print(caps.get(“PA”));//Harrisburg print(caps.get(“WV”));//null, not in map ob=caps.put(“NY”,”Albany”); print(ob);//New York City (old value) print(caps.containsKey(“MD”)); //True print(caps.size()); //3 ob=caps.remove(“PA”); print(ob); //Harrisburg

Summary Searching involves retrieving information about an object Two main applications –Sets - Does this belong –Map - What information is stored with this? Need to be able to tell if two object are equal –All objects in Java implement the equals() method.