Sets and Maps Ellen Walker CPSC 201 Data Structures Hiram College.

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.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Fall 2007CS 225 Sets and Maps Chapter 9. Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
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.
Sets and Maps (and Hashing)
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 225 Sets and Maps Chapter 7. Fall 2007CS 225 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
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.
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.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Sets and Maps Starring: keySet Co-Starring: 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.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
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.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Space vs. time  space/time tradeoffs  hashing  hash table, hash function  linear probing.
COMP 103 Bitsets. 2 Sets, and more Sets!  Unsorted Array  Sorted ArrayO(n) for at least one of  Linked Listcontains, add, remove  Binary Search TreeO(log.
Elementary Data Organization. Outline  Data, Entity and Information  Primitive data types  Non primitive data Types  Data structure  Definition 
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
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.
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.
AD Lecture #3 Java Collection Framework 1.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
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
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
CSE 143 Lecture 11: Sets and Maps reading:
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
CS2005 Week 7 Lectures Set Abstract Data Type.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Sets and Maps Chapter 9.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Efficiency of in Binary Trees
TCSS 342, Winter 2006 Lecture Notes
Part of the Collections Framework
Maps.
Sets, Maps and Hash Tables
CS2110: Software Development Methods
CSE 1020: The Collection Framework
Sets and Maps Chapter 9.
Data Structures II AP Computer Science
Sets and Maps Chapter 7 CS 225.
Presentation transcript:

Sets and Maps Ellen Walker CPSC 201 Data Structures Hiram College

Sets in the Collection Hierarchy

Set vs. List A list is a sequence of items –Order matters –Traversal makes sense –Duplicates are allowed in the list A set is a collection of items, without sequence –Order does not matter; traversal does not make sense –No duplicates are allowed in the list Like ordered lists and binary search trees, sets are value-oriented

Mathematical Set Operations Element-of (x  S) –Boolean: Is x a member of the set S? Subset (S  T) –Boolean: Are all elements of S also elements of T? Union (S  T) –Create a new set with all elements of S and all elements of T Intersection (S  T) –Create a new set with all elements that are in both S and T Set-Difference (S – T) –Create a new set with all elements that are in S but not in T

Methods of Set

Implementing Mathematical Operations Element-of (x  S) –S.contains(x); Subset (S  T) –T.containsAll(S); Union (S  T) –S.addAll(T); or T.addAll(S); Intersection (S  T) –S.retainAll(T) or T.retainAll(S); Set-Difference (S – T) –S.removeAll(T);

Create and Print a Set HashSet students201 = new HashSet (); String[] names = {"ChrisK", "ChrisF", "Robin", "Tim", "Andrew", "David", "Will", "Matt"}; for(int i=0;i<names.length;i++) students201.add(names[i]); System.out.println("Students in 201 are: ” + students201);

Compute Union and Intersection HashSet inBoth = (HashSet )students356.clone(); inBoth.retainAll(students201); //intersection System.out.println("Students in both 201 and 356 are: " + inBoth); HashSet inOne = (HashSet )(students356.clone()); inOne.addAll(students201); //union System.out.println("Students in one of the classes are: " + inOne);

Set Iteration Iterator presents elements of set in arbitrary order Because there is an iterator, we can use the foreach loop with sets: for (String x : students201) System.out.println(x);

Maps A map is a set of ordered (key, value) pairs Given the key, we should be able to find the value Example: –{ (CPSC, Computer Science), (MATH, Mathematics), (INTD, Interdisciplinary) (CS, Computer Science) }

Graphical View CPSC CS MATH INTD Computer Science Mathematics Interdisciplinary Arrows connect key to value This is a many-to-one mapping (vs. one-to-one)

Methods of Map

Maps and Sets are Similar… In both, order doesn’t matter For both, need to determine whether an object is present (key, for a map) Duplicates are not allowed (duplicate keys not allowed for maps) Maps are more complicated because the association between key and value must be maintained

Corresponding Map and Set Methods Map Methods V get (Object key) V put(K key, V value) V remove (Object key) Set Methods boolean contains(Object key) boolean add (K key) boolean remove(K key)

Implementing Maps and Sets TreeMap and TreeSet –Implemented using Binary Search Tree (actually a red-black tree) HashMap and HashSet –Implemented using Hash Table

Hash Codes in Java Object.hashCode() –Based on address in memory String.hashCode() –Multiply each character’s code by 31 raised to a power depending on position –Example hash(“EAT”) = hash(‘E’)* hash(‘A’)*31 + hash(‘T’) –Hash(“EAT”) ≠ hash(“ATE”) ≠ hash(“TEA”)

.hashCode() and.equals() Java contract for.hashCode –If obj1.equals(obj2) then obj1.hashCode() == obj2.hashCode() Object’s.equals and.hashCode() depend on address Many objects (e.g. Integer, String) override both.equals() and.hashCode() to depend on value If your class overrides.equals(), it should also override.hashCode()