1 Sets and Maps Starring: keySet Co-Starring: Collections.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
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.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
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.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng 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.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
(c) University of Washington14-1 CSC 143 Java Collections.
1 Hashing Starring: HashSet Co-Starring: HashMap.
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.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Data structures Abstract data types Java classes for Data structures and ADTs.
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.
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,
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Interfaces in Java’s Collection Framework Rick Mercer.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
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.
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.
1 Hashing Starring: HashSet Co-Starring: HashMap.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
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
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Collections Dwight Deugo Nesa Matic
© 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:
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.
Java Collections Framework The client view. The Big Picture.
Java Collections CHAPTER 3-February-2003
Efficiency of in Binary Trees
University of Central Florida COP 3330 Object Oriented Programming
Road Map CS Concepts Data Structures Java Language Java Collections
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
Java Collections Framework
CS2110: Software Development Methods
Introduction to Collections
L5. Necessary Java Programming Techniques
CSE 1020: The Collection Framework
Hashing in java.util
Data Structures II AP Computer Science
Presentation transcript:

1 Sets and Maps Starring: keySet Co-Starring: Collections

2 Purpose: In this lecture we discus two interfaces, the Set and the Map and sets up our discussion of HashSet, HashMap, TreeMap & TreeSet

3 Resources: Barrons Chapter 11 p.368 (Only Collections, Sets & Maps p & p.377 & p.382) Java Essentials Study Guide Chapter 17 p.303 Java Fundanentals, Lambert Chapter 17 p.567

4 Handouts: 1.CreateMySet.java 2.Map-Key-Value.java 3.Sets Maps and the ADTs.doc 4.set map interfaces.Doc

5 Intro: Two ADT’s Set and Map provide rules for creating Data Structures that conform to specific behaviors

6 We are required to understand the requirements of these two interfaces and then, in the next few lectures, we will discuss and work with the following implementations of these interfaces: HashSet HashMap TreeMap TreeSet

7 In this Lecture we will discuss: The Collections API Set Interface Map Interface

8 Collections: Collections are simply a group of objects There are collections that permit duplicate objects while others do not Some collections order the objects while others do not

9 A collection data type has the following behaviors: insert elements remove elements iterate over the elements in the collection

10

11 Set: Set is a collection Set is not ordered Set does NOT allow duplicate elements Set may have a null element

12 Insert a unique object / element into the Set Remove an object / element from the Set Determine if and object / element is in the Set Use the Iterator to traverse the elements in the Set

13 Hashset (hash table) and TreeSet (BST) implement the Set Interface interface java.util.Set Required methods boolean add(Object x) adds element if unique otherwise leaves set unchanged

14 boolean contains(Object x) determines if a given object is an element of the set boolean remove(Object x) removes the element from the set or leaves set unchanged int size( ) number of elements in the set Iterator iterator( ) allows for set traversal

15 Map: Map is not a real collection, they Produce Collections Maps keys to values Map does NOT allow duplicate elements as each Key in a Map has only one (a unique) Value However, different Keys can map to the same object (value)

16 The Key and the Value can be any object Insert a key / value pair into a Map Obtain a value thru its Key Determine if a Target Key is in the Map

17 Traverse the elements of the Map using the keySet method Iterate thru the Map elements (iterate using the Keys) The TreeMap and the HashMap implement the Map Interface interface java.util.Map (AB only)

18 Required methods: Object put(Object key, Object value) Associates a Value with a Key and places this pair into the Map REPLACES a prior value if the Key already is Mapped to a value Returns the PREVIOUS Key associated value or NULL if no prior mapping exists

19 Object get(Object key) Returns the value associated with a Key OR NULL if no map exists or the Key does map to a NULL Object remove (Object key) Removes the map to this Key and returns its associated value OR returns NULL if no map existed or mapping was to NULL

20 boolean containsKey(Object key) True if there is a key / value map otherwise false int size( ) Returns the number key / value mappings Set keySet( ) Retuns the Set of keys in the map

21 You can map: Names to phone numbers College friends to the school they attend Animals to animal sounds Coin name to its value Car model to its make Log in IDs to Passwords

22 The keySet produces a Set of keys from which we can visit all of the elements of a HashMap or a TreeMap We can visit all of the values (elements) by iterating over the key Set that is returned from the call to the Map’s keySet method

23 The following class contains Key / Value elements Student ID is the Key & the Name is the Value

24 public class Student { Integer id; String name; public Student i, String n) { id = new Integer(i); name = n; } public Integer getId() { return id; } public String getName() { return name; } public String toString() { return id.toString() + ", " + name; }

25 Illustration of a Map using the HashMap class: Map stuffMap = new HashMap(); myStuff[] ms = new Student[5]; ms[0] = new Student(21,"Farrell"); ms[1] = new Student (31,"Castro"); ms[2] = new Student (11,"Defazio"); ms[3] = new Student (61,"Zegers"); ms[4] = new Student (86,"Rogers"); for (int t=0; t < 5; t++) { stuffMap.put(ms[t].getId(), ms[t]); } for (int t=0; t < 5; t++) { myStuff x = (Student)stuffMap.get(ms[t].getId()); System.out.println(x.toString()); }

26 MAP accepts "elements" as 2 separate objects, a key and the data A MAP ADT must conform to the following: Put Associates the specified value with the specified key in this map If the map previously contained a mapping for this key, the old value is replaced.

27 Get -- Returns the value to which this map maps the specified key Remove -- Removes the mapping for this key from this map if present

28 Containskey - Returns true if this map contains a mapping for the specified key Keyset - Returns a set view of the keys contained in this map.

29 AP AB Subset Requirements: Understand the requirements, restrictions and behaviors of the Set and Map ADT’s You Will Not be required to implement the Set or Map Interface But you WILL be required to work with the HashSet, TreeSet, HashMap & TreeMap (Discussed in Next Lecture)

30 Project:Create Your Own Set and Map Classes

31 NO TEST FOR THIS LECTURE !!!