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.

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
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 CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
15-Jun-15 Lists in Java Part of the Collections Framework.
Professor Evan Korth (adapted from Sun’s collections documentation)
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.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
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.
Using Maps. 2 A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable,
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
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.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
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.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
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.
The Java Collections Framework Based on
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:
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.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Set and Map IS 313, Skeletons  Useful coding patterns  Should understand how and why they work how to use  possible quiz material.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
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.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Collections. 2 Concept A collection is a data structure – actually, an object – to hold other objects, which let you store and organize objects in useful.
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
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”
Java Collections CHAPTER 3-February-2003
Java Collections OOP tirgul No
Efficiency of in Binary Trees
Programming & Data Structures
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
TCSS 342, Winter 2006 Lecture Notes
Lecture 6: Collections.
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
Collections in Java The objectives of this lecture are:
TCSS 342, Winter 2005 Lecture Notes
Introduction to Collections
Introduction to Collections
Hashing in java.util
Data Structures II AP Computer Science
Part of the Collections Framework
Set and Map IS 313,
Presentation transcript:

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 s int size( ); boolean isEmpty( ); boolean contains(Object e); boolean add(Object e); boolean remove(Object e); Iterator iterator( ); boolean containsAll(Collection c); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear( ); Object[ ] toArray( ); Object[ ] toArray(Object a[ ]);

Iterators for sets A set has a method Iterator iterator( ) to create an iterator over the set The iterator has the usual methods: –boolean hasNext() –Object next() –void remove() remove() allows you to remove elements as you iterate over the set If you change the set in any other way during iteration, the iterator will throw a ConcurrentModificationException

Set implementations Set is an interface; you can’t say new Set ( ) There are two implementations: –HashSet is best for most purposes –TreeSet guarantees the order of iteration It’s poor style to expose the implementation, so: Good: Set s = new HashSet( ); Bad: HashSet s = new HashSet( );

Typical set operations Testing if s2 is a subset of s1 s1.containsAll(s2) Setting s1 to the union of s1 and s2 s1.addAll(s2) Setting s1 to the intersection of s1 and s2 s1.retainAll(s2) Setting s1 to the set difference of s1 and s2 s1.removeAll(s2)

Set equality Object.equals(Object), inherited by all objects, really is an identity comparison Implementations of Set override equals so that sets are equal if they contain the same elements equals even works if two sets have different implementations equals is a test on entire sets; you have to be sure you have a working equals on individual set elements hashCode has been extended similarly

Membership testing in HashSet s When testing whether a HashSet contains a given object, Java does this: –Java computes the hash code for the given object We’ll discuss hash codes later Java compares the given object, using equals, only with elements in the set that have the same hash code Hence, an object will be considered to be in the set only if both: –It has the same hash code as an element in the set, and –The equals comparison returns true Moral: to use a HashSet properly, you must have a good public int hashCode() defined for the elements of the set

The SortedSet interface A SortedSet is just like a Set, except that an Iterator will go through it in a guaranteed order Implemented by TreeSet

Membership testing in TreeSet s In a TreeSet, elements are kept in order That means Java must have some means of comparing elements to decide which is “larger” and which is “smaller” Java does this by using the int compareTo(Object) method of the Comparable interface For this to work properly, compareTo must be consistent with equals Moral: to use a TreeSet properly, you must implement both the equals method and the Comparable inteface for the elements of the set

Set tips add and remove return true if they modify the set Here's a trick to remove duplicates from a Collection c : –Collection noDups = new HashSet(c); A Set may not contain itself an an element Danger: the behavior of a set is undefined if you change an element to be equal to another element

The Map interface A Map is an object that maps keys to values A map cannot contain duplicate keys Each key can map to at most one value Examples: dictionary, phone book, etc.

Map implementations Map is an interface; you can’t say new Map ( ) Here are two implementations: –HashMap is the faster –TreeMap guarantees the order of iteration It’s poor style to expose the implementation, so: Good: Map map = new HashMap ( ); Bad: HashMap map = new HashMap ( );

Map: Basic operations Object put(Object key, Object value); Object get(Object key); Object remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size( ); boolean isEmpty( );

More about put If the map already contains a given key, put(key, value) replaces the value associated with that key This means Java has to do equality testing on keys With a HashMap implementation, you need to define equals and hashCode for all your keys With a TreeMap implementation, you need to define equals and implement the Comparable interface for all your keys

Map: Bulk operations void putAll(Map t); –copies one Map into another void clear();

Map: Collection views public Set keySet( ); public Collection values( ); public Set entrySet( ); –returns a set of Map.Entry (key-value) pairs You can create iterators for the key set, the value set, or the entry set The above views provide the only way to iterate over a Map

Map.Entry: Interface for entrySet elements public interface Entry { Object getKey( ); Object getValue( ); Object setValue(Object value); } This is a small interface for working with the Collection returned by entrySet( ) Can get elements only from the Iterator, and they are only valid during the iteration

The End