Set and Map IS 313, 1.16.2002.

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.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
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
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.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
111 © 2002, Cisco Systems, Inc. All rights reserved.
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.
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
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.
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.
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 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
CSE 143 Lecture 11: Sets and Maps reading:
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Java Collections CHAPTER 3-February-2003
Java Collections OOP tirgul No
Fundamental of Java Programming
Using the Java Collection Libraries COMP 103 # T2
Chapter 19 Java Data Structures
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Efficiency of in Binary Trees
Programming & Data Structures
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
Back to Collections Lists: Sets Maps ArrayList LinkedList
Using Maps.
Containers ב Java אליהו חלסצ'י תכנות מתקדם תרגול מספר 3
TCSS 342, Winter 2006 Lecture Notes
Lecture 6: Collections.
Part of the Collections Framework
Using Maps.
Maps "He's off the map!" -Stan (Mark Ruffalo) Eternal Sunshine of the Spotless Mind.
Collections in Java The objectives of this lecture are:
"He's off the map!" - Eternal Sunshine of the Spotless Mind
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
L5. Necessary Java Programming Techniques
CSE 1020: The Collection Framework
Introduction to Collections
slides created by Marty Stepp
Hashing in java.util
Data Structures II AP Computer Science
Presentation transcript:

Set and Map IS 313, 1.16.2002

Tuesday’s code Comparator

Skeletons Useful coding patterns Should understand how and why they work how to use possible quiz material

Homework Late policy Academic honesty Rule of thumb Note 10% per day only if help requested 48 hrs in advance Academic honesty No code sharing BUT discussion and assistance is OK Rule of thumb Am I getting advice? Or a homework solution? Note Plagiarized code is (surprisingly) easy to detect

Resources Homework discussion forum Email to instructor if you need to get advice on a large chunk of code

Homework #1 Frequent Flyer Program Input Output A file of member accounts A file of flights Output A updated file of member accounts note: must be sorted by member name

Homework 1 Solution Structure Member class implement comparable or, separate CompareByName comparator MemberMap to hold Members encapsulates a SortedMap access by id Flight stores flight information including encapsulates List of member #s

Homework #1 Solution Structure FlightList encapsulates a List all of the flight objects in the file MemberUpdater “main” class should be VERY simple

Part 2 Solution Structure CapacityInfo class Encapsulated SortedMap associating dates with capacity data

Main Program Very simple public class MemberUpdater { public void main (String [] args) MemberList members = new MemberList (); members.load (“accounts.dat”); FlightList flights = new FlightList (); flights.load (“flights.dat”); members.updateForFlights (flights); members.save (“updated.dat”); }

General Advice Start early Implement Member first Don’t cut corners working on homework = studying for quiz Implement Member first Use instantiation skeleton (#1 and #2) to create from file Don’t cut corners all 6 classes are needed

StringTokenizer Useful for handling text files Breaks strings into tokens usually defined by whitespace “words”

StringTokenizer cont’d Works like an iterator Test for completion hasMoreTokens Get token and move nextToken Skeleton #2 shows how to use a StringTokenizer to create objects from a text file

Example String text = "Whatever you want, you'll get it."; StringTokenizer st = new StringTokenizer(text); while( st.hasMoreTokens() ) { String word = st.nextToken(); ...process token... }

Example StringTokenizer

Fundamental Abstractions Collection Any grouping of items Set Unordered, non-redundant List Ordered Map Object->Object mappings

Collection Interfaces

Set Has only the Collection methods But the Collection has no duplicates

Remove Duplicates How does this work? public Object [] removeDups (Object [] ar) { List original = Arrays.asList (ar); Set noDups = new HashSet(original); return noDups.toArray(); } How does this work? Arrays utility class Will the returned array be in the same order as the original?

SortedSet Set starts to look like a list Like sorting operations Elements are iterated over in order Like sorting operations Objects must be Comparable Or a Comparator can be supplied

SortedSet API public interface SortedSet extends Set { // Range-view SortedSet subSet(Object fromElement, Object toElement); SortedSet headSet(Object toElement); SortedSet tailSet(Object fromElement); // Endpoints Object first(); Object last(); // Comparator access Comparator comparator(); }

Example SortedSet Comparable

Map Collection of pairs We can associate values Key, value We can associate values name, phone number member id, member object Both key and value must be objects use “wrapper” classes for primitive types map.put (new Integer(5), “stored at 5”);

Map API 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(); // Bulk Operations void putAll(Map t); void clear(); // Collection Views public Set keySet(); public Collection values(); public Set entrySet();

Collection Views keySet() values() entrySet() A set of the keys A collection of the values entrySet() A set of all the pairs Entry objects getKey, getValue

Views Views are still tied to the original Map Example If I remove a key from the keySet It is removed from the Map Example m1.keySet().removeAll(m2.keySet()); Removes any entries in m1 that have the same key as an entry in m2.

SortedMap Exactly analogous to SortedSet Sort is in key order

Implementations Set Map HashSet TreeSet HashMap TreeMap (WeakHashMap) (LinkedHashMap)

HashSet Collection implemented with a hash table O(1) access Provide you size it appropriately An odd # about twice as big as the size of the largest set

Hash Table

To store a set in a hash table Use hash key = the item in the set stored value = anything If there is an entry in the table the item is in the set

TreeSet Only useful for sorted sets O(log n) for access but it is always sorted

Binary Tree

To store a set in a binary tree Use tree key = item in the set value = anything If the tree key is in the tree the item is in the set To iterate in order start at the left of the tree “in-order” traversal

Trade-offs Sorting Size for large collections

HashMap O(1) access Same size considerations as HashSet

To store a map in a hash table Use hash key = map key value = value To look up a value find the key/value pair return value

TreeMap Only useful for SortedMap O(log n) access

To store a map in a binary tree Use tree key = map key tree value = map value To look up a value traverse tree to appropriate key return stored value To iterate “in-order” tranversal

Decorators Unmodifiable Synchronized Pass in a normal collection (set, map, whatever) The resulting collection is the same but can’t be modified Synchronized Pass in a normal collection The resulting collection is synchronized Can’t be shared between threads Useful in multi-threaded programs, otherwise inefficient

Example user interface component could modify list instead: List myList = new LinkedList (); ... manipulate list ... display (myList); user interface component could modify list instead: display (Collections.unmodifiableList (myList));

Example Map values() collection Inheritance HashMap TreeMap values() collection Inheritance Encapsulation / Composition Method overloading