Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading: 13.1-13.6.

Slides:



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

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.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
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.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
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.
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.
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.
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.
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.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Big Java Chapter 16.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Chapter 18 Java Collections Framework
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,
Hashing as a Dictionary Implementation Chapter 19.
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.
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.
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.
Abstract Data Type EnhanceEdu.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
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
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
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.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Java Collections Framework The client view. The Big Picture.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Tables and Priority Queues
Sets and Maps Chapter 9.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Efficiency of in Binary Trees
Week 8 - Friday CS221.
A tree set Our SearchTree class is essentially a set.
Data Structures TreeMap HashMap.
Road Map CS Concepts Data Structures Java Language Java Collections
TreeSet TreeMap HashMap
Part of the Collections Framework
A tree set Our SearchTree class is essentially a set.
Maps "He's off the map!" -Stan (Mark Ruffalo) Eternal Sunshine of the Spotless Mind.
L5. Necessary Java Programming Techniques
Sets and Maps Chapter 9.
Hashing in java.util
Trees in java.util A set is an object that stores unique elements
Data Structures II AP Computer Science
Presentation transcript:

Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:

Sets A set is a collection of elements with no duplicates We had a set application in Lab 3 where we needed a data structure to represent a drum full of Bingo balls for random drawing Since there should only be one Bingo ball with each number, the correct type of collection is a set

Maps A map is a collection that establishes a relationship between keys and values The implementation should provide an efficient way to retrieve a value given its key There must be a one-to-one mapping from a key to a value – each key must have only one value, but multiple keys can have the same value Therefore, there isn’t necessarily a one-to-one mapping from a value to a key

Map Entry Class To implement a map collection using any data structure, we need a class for objects that link a key with its corresponding value MappingClass Entry - key : K - value : V + Entry(key : K, value : V) + usual setters/getters {some data structure containing Entry objects} + normal Map methods KeyClassValueClass

Map Entry Class The Entry class is not used outside its Map class The Entry class code is usually written as an inner class of the Map class that it supports

6 The Comparator Interface In the Java Collections API, either the Comparator or Comparable interface may be used A Comparator class object can be passed to the collection class’s constructor to use in comparing The Comparator’s “compare” method takes two objects as parameters and returns a value like the Comparable compareTo method does ( 0 representing ) The compare method is not implemented within the key class but uses two objects of that class

The Comparator Interface Implementing a Comparator for Strings that uses their length as the basis for the comparison public class StringComparator implements Comparator { public int compare(String s1, String s2) { return s1.length() – s2.length(); }

8 Java Collections API: Implementing Sets and Maps The Java class library provides thorough and efficient implementations of underlying binary search trees in these two classes: – TreeSet – TreeMap Both of those classes can be used with either the normal ordering of the elements (via the Comparable interface) or via a Comparator

9 TreeSet In a TreeSet, we store elements in an order determined either by their natural ordering (based on their CompareTo method) or an ordering based on a provided Comparator Each element stored in a TreeSet contains all of the data associated with that object The TreeSet class implements a set using a Red/Black binary search tree for efficiency in the add, contains, and remove operations

10 TreeSet Some of the TreeSet unique methods are: TreeSet() // constructs a new set sorted according to natural order of the objects TreeSet (Comparator c) // constructs a new set sorted according to Comparator c boolean add(T o) // adds the specified element to the set if not already present boolean contains(Object o) // returns true if this object is present in the set boolean remove(Object o) // removes this element from the set if it is present

11 TreeMap In a TreeMap, we separate the data being stored into a key and the rest of the data (the value) Internally, node objects are stored in the tree Each node object contains – a reference to the key – a reference to the object containing the rest of the data – two links to the child nodes – and a link to the parent node The TreeMap class implements a map using a Red/Black binary search tree

12 TreeMap Some of the TreeMap unique methods are: TreeMap ()// constructs a new map sorted according to natural order of the objects TreeMap (Comparator c) // constructs a new map sorted according to Comparator c V put(K key, V value) // associates the value with the key boolean containsKey(Object key) // returns true if the map contains a mapping for key boolean containsValue(Object value) // returns true if the mapping contains a key value pair for this value V get(Object key) // returns the value V mapped to the key

Using Set/Map APIs with a Comparator Instantiate the Comparator Comparator comp = new StringComparator(); Instantiate a TreeSet containing Strings TreeSet mySet = new TreeSet (comp); Instantiate a TreeMap with Strings as keys TreeMap myTree = new TreeMap (comp);

Set and Map Efficiency The TreeSet and TreeMap classes provide O(log n) access to their data When the sort order is not important, there is a more efficient way to implement sets and maps with a data structure called a hash table A hash table provides approximately O(1) access to its data and will be covered in CS310

Review for Exam Review for Exam 3 – Practice exam is on-line – Question and Answer Session Exam 3 – Open Book / Open Notes – No Electronic Devices (calculators, laptops, etc) – Students with E-books must sit in front row so that I can see what’s on their screen at any time