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.

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.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
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.
15-Jun-15 Lists in Java Part of the Collections Framework.
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.
Java Collection Framework. Interface Collection add(o) Add a new element clear() Remove all elements contains(o) Membership checking. IsEmpty() Whether.
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.
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
The Java Collections Package C. DeJong Fall 2001.
Java's Collection Framework
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Advanced Java Session 3 New York University School of Continuing and Professional Studies.
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.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Big Java Chapter 16.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
Data structures and algorithms in the collection framework 1.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
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
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
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:
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
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.
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 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
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
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
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Java Collections CHAPTER 3-February-2003
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Sets Set is an unordered list of items
Ch 16: Data Structures - Set and Map
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
TCSS 342, Winter 2006 Lecture Notes
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
Introduction to Collections
Introduction to Collections
Data Structures II AP Computer Science
Part of the Collections Framework
Presentation transcript:

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 trees Heap Priority queue We will only discuss Set and Map

Sets Unordered collection Fundamental operations: –Add an element –Remove an element –Containment test (is object in set?) –List elements (in arbitrary order) Reject duplicates

Set Implementation > Set HashSetTreeSet add(E) addAll(Collection) clear() contains(Object) containsAll(Collection) equals(Object) hashCode() isEmpty() iterator() remove(Object) removeAll(Collection) retainAll(Collection) size() toArray() toArray(T[]) UML reminder: dotted line, open triangle pointing to interface

Simple SetDemo import java.util.*; public class SetDemo { private Set animals; public SetDemo() { animals = new HashSet (); } public void printAnimals(){ for (String animal : animals) System.out.println(animal); } instantiate with specific implementation. Advantage: could easily change implementation, rest of program stays the same. Nodes are not necessarily visited in order inserted!

Add input and main methods public void getAnimals() { Scanner in = new Scanner(System.in); String animal = ""; do { System.out.print("Enter an animal or Q to quit: "); animal = in.next(); if (!(animal.equalsIgnoreCase("Q"))) animals.add(animal); } while (!(animal.equalsIgnoreCase("Q"))); } public static void main(String[] args) { SetDemo demo = new SetDemo(); demo.getAnimals(); demo.printAnimals(); } can use methods from interface, such as add. Also have remove, contains, clear, size and more TRY: giraffe, bear, coyote, lion

Maps A map data type keeps associations between keys and values Cannot contain duplicate keys Operations include: –put –get –containsKey/containsValue –keySet/values – return all keys/values –size Java has two implementations: HashMap and TreeMap.

Map Demo import java.util.*; public class MapDemo { Map sightings; public MapDemo() { sightings = new HashMap (); } public void loadSightings() { sightings.put("fox", 10); sightings.put("bear", 2); sightings.put("deer", 60); sightings.put("elk", 30); } public void printSightings() { Set keySet = sightings.keySet(); for (String key : keySet) System.out.println(key + "->" + sightings.get(key)); } public static void main(String[] args) { MapDemo demo = new MapDemo(); demo.loadSightings(); demo.printSightings(); }

TreeSet or HashSet? With a good hash function, hashing is usually faster Balanced trees (remember those?) can guarantee reasonable performance, HashSet depends entirely on performance of hash function To use TreeSet, objects being stored must implement Comparable interface For TreeMap, same requirement for keys Can supply a Comparator object to TreeSet/TreeMap constructor (takes two objects and returns comparison) TreeSet is preferable if want to print list of items in order

TreeSet needs Comparable items public class Student implements Comparable { private String name; public Student(String name) { super(); this.name = name; } // Include setters and getters public int compareTo(Student other) { return name.compareTo(other.name); }

TreeSet Example public class StudentSet { private Set students; public StudentSet() { students = new HashSet (); } public void addStudent(Student s) { students.add(s); } public void displayStudents() { for (Student s : students) { System.out.println(s.getName()); } public static void main(String[] args) { StudentSet set = new StudentSet(); set.addStudent(new Student("Cyndi")); set.addStudent(new Student("Bill")); set.addStudent(new Student("Jane")); set.displayStudents(); }