CS2110: Software Development Methods

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
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
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.
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.
(c) University of Washington14-1 CSC 143 Java Collections.
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.
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.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
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]
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
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 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
Collections Dwight Deugo Nesa Matic
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
CSE 143 Lecture 11: Sets and Maps reading:
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Java Collections CHAPTER 3-February-2003
Sets and Maps Chapter 9.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Sixth Lecture ArrayList Abstract Class and Interface
October 2nd – Dictionary ADT
Chapter 19 Java Data Structures
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Ch 16: Data Structures - Set and Map
Efficiency of in Binary Trees
Week 8 - Wednesday CS221.
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
Week 8 - Friday CS221.
Data Structures TreeMap HashMap.
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
Road Map CS Concepts Data Structures Java Language Java Collections
TreeSet TreeMap HashMap
TCSS 342, Winter 2006 Lecture Notes
Sets (11.2) set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains)
CSE 373: Data Structures and Algorithms
Part of the Collections Framework
Java Collections Framework
Maps.
Maps "He's off the map!" -Stan (Mark Ruffalo) Eternal Sunshine of the Spotless Mind.
EE 422C Sets.
Sets, Maps and Hash Tables
14.1 The java.util Package.
Collections Not in our text.
"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.
Collections Framework
CSE 1020: The Collection Framework
Sets (11.2) set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains)
Sets and Maps Chapter 9.
slides created by Marty Stepp
Web Design & Development Lecture 6
Introduction to Java Collection
Presentation transcript:

CS2110: Software Development Methods Maps and Sets in Java These slides are to help with the lab, Finding Your Way with Maps More on these topics in lecture. Today’s lab uses Maps (and Sets but just a little).

Lists, Sets and Maps in Java Java has what’s called a Collections Framework Includes very useful classes like ArrayList Also, other useful classes for other data types Sets Data model: a collection of items Operations: membership, insert, delete Maps: like a “lookup table” Data model: each data item stored with a lookup key Operations: retrieve with the key, etc.

Collection Interface All collections implement the Collection interface. Helpful! all the collection classes have common operations! boolean add(Object obj); Iterator iterator(); int size(); boolean isEmpty(); boolean contains(Object obj); boolean containsAll (Collection other); … See Java API documentation for all methods

Set and HashSet in Java Collections A Set is a collection of items that are unordered are unique (no duplicates) What kind of operations do we think of? element membership (add, remove, isMember, etc.) manipulation (union, intersection,…) Java gives us: Set – an interface Several implementation classes, including HashSet

Java’s Set Interface (see MSD, p. 625) The Set interface looks just like the Collection interface, but with more specific behaviors boolean add(elem) – returns false if already there boolean remove(elem) – returns false if not there What’s sweet here: Try to add something that’s already there? Remove something that’s not there? No problem! It basically ignores that attempt! This allows some powerful methods to be very short See examples on pages 625 and 626

Common Situation: “data lookup” We have some some information, and we want to retrieve it or look it up We find it quickly by using some special data value. Let’s call that a key Examples of keys and data values used in lookup? Often use term “lookup table” for these. Databases work this way, don’t they?

What if we had a “Type” for a Table? Called “Map” in Java: find data given some key value Map’s model of information Some set of values, each associated with a key Operations Lookup: given a key, retrieve the values Insert, remove, updates, etc. How could we implement this? You’ve been using lists (arrays, ArrayLists) and the contains() and indexOf() methods Disadvantages?

Java Type: Maps Maps Like sets, but have <key, value> instead of <value> Examples in real life? Dictionary: word to its definition (or list of definitions) email to Facebook Profile Host Names to IP addresses How to declare: Map<KeyType,ValueType>… Examples: Map<String,Integer> // maps string to a number Map<Student,String> // maps Student to a String Map<String,ArrayList<Course>> // e.g. a schedule

Important Map Methods Keys and values put (key, value), get(key), remove(key) containsKey(key), containsValue(value) Can think of a Map as a set of keys and a set of values keySet() // returns a Set of key values values() // returns a Collection of values Others methods too! See doc or a Java reference.

Concrete Classes Map is an interface. We need concrete classes that implement it. HashMap Most commonly used. Allows nulls as values. Need a hashCode() method String and other classes Java gives us have this More later on doing this for our own classes TreeMap Uses a search tree data structure Values stored in a sorted order More on this later.

Why Are Maps Useful and Important? “Lookup values for a key” -- an important task We often want to do this! Need flexibility for what values are stored for a given key Need to use objects other than Strings or ints as key values Efficiency matters for large sets of data These are fast!

Demo Download the MapLab.zip file from the lab page In Eclipse, do the following: Select Import from the File menu Select “General” and then “Existing Projects into Workspace” Choose “Select Archive File” and the find the MapLab.zip file Two files in there: MapDemo0.java – the demo the TAs will do now MapDemo.java – the lab exercise you and your partner will do afterwards

MapDemo0.java file We define a set and a map We get a scanner for a file that looks like this: Cavman 101 JPA, Apt. 3. C'ville, VA 22900 Tom Monticello, C'ville, VA 22900 Task 1: store the names in the set Task 2: store (name,address) pairs in the map Task 3 (if time): split the address-line into words and store each of them in the set Task 4: read key and look it up in Map