1 sets and maps starring: keyset co-starring: collections

31
1 Sets and Maps Starring: keySet Co-Starring: Collections

Upload: winifred-nichols

Post on 31-Dec-2015

234 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: 1 Sets and Maps Starring: keySet Co-Starring: Collections

1

Sets and Maps

Starring: keySet

Co-Starring: Collections

Page 2: 1 Sets and Maps Starring: keySet Co-Starring: Collections

2

Purpose:

In this lecture we discus two interfaces, the Set and the Map and sets up our discussion of HashSet, HashMap, TreeMap & TreeSet

Page 3: 1 Sets and Maps Starring: keySet Co-Starring: Collections

3

Resources:

Barrons Chapter 11 p.368 (Only Collections, Sets & Maps p.368-369 & p.377 & p.382)

Java Essentials Study Guide Chapter 17 p.303

Java Fundanentals, Lambert Chapter 17 p.567

Page 4: 1 Sets and Maps Starring: keySet Co-Starring: Collections

4

Handouts:

1.CreateMySet.java

2.Map-Key-Value.java

3.Sets Maps and the ADTs.doc

4.set map interfaces.Doc

Page 5: 1 Sets and Maps Starring: keySet Co-Starring: Collections

5

Intro:

Two ADT’s Set and Map provide rules for creating Data Structures that conform to specific behaviors

Page 6: 1 Sets and Maps Starring: keySet Co-Starring: Collections

6

We are required to understand the requirements of these two interfaces and then, in the next few lectures, we will discuss and work with the following implementations of these interfaces:

HashSetHashMapTreeMapTreeSet

Page 7: 1 Sets and Maps Starring: keySet Co-Starring: Collections

7

In this Lecture we will discuss:

The Collections API Set Interface Map Interface

Page 8: 1 Sets and Maps Starring: keySet Co-Starring: Collections

8

Collections:Collections are simply a group of

objects

There are collections that permit duplicate objects while others do not

Some collections order the objects while others do not

Page 9: 1 Sets and Maps Starring: keySet Co-Starring: Collections

9

A collection data type has the following behaviors:

insert elements remove elements iterate over the elements in the

collection

Page 10: 1 Sets and Maps Starring: keySet Co-Starring: Collections

10

Page 11: 1 Sets and Maps Starring: keySet Co-Starring: Collections

11

Set:

Set is a collection

Set is not ordered

Set does NOT allow duplicate elements

Set may have a null element

Page 12: 1 Sets and Maps Starring: keySet Co-Starring: Collections

12

Insert a unique object / element into the Set

Remove an object / element from the Set

Determine if and object / element is in the Set

Use the Iterator to traverse the elements in the Set

Page 13: 1 Sets and Maps Starring: keySet Co-Starring: Collections

13

Hashset (hash table) and TreeSet (BST) implement the Set Interface

interface java.util.Set

Required methods

boolean add(Object x)

adds element if unique otherwise leaves set unchanged

Page 14: 1 Sets and Maps Starring: keySet Co-Starring: Collections

14

boolean contains(Object x) determines if a given object is an

element of the set

boolean remove(Object x) removes the element from the set or

leaves set unchanged

int size( ) number of elements in the set

Iterator iterator( ) allows for set traversal

Page 15: 1 Sets and Maps Starring: keySet Co-Starring: Collections

15

Map:Map is not a real collection, they Produce

Collections

Maps keys to values

Map does NOT allow duplicate elements as each Key in a Map has only one (a unique) Value

However, different Keys can map to the same object (value)

Page 16: 1 Sets and Maps Starring: keySet Co-Starring: Collections

16

The Key and the Value can be any object

Insert a key / value pair into a Map

Obtain a value thru its Key

Determine if a Target Key is in the Map

Page 17: 1 Sets and Maps Starring: keySet Co-Starring: Collections

17

Traverse the elements of the Map using the keySet method

Iterate thru the Map elements (iterate using the Keys)

The TreeMap and the HashMap implement the Map Interface

interface java.util.Map (AB only)

Page 18: 1 Sets and Maps Starring: keySet Co-Starring: Collections

18

Required methods:

Object put(Object key, Object value) Associates a Value with a Key and places

this pair into the Map

REPLACES a prior value if the Key already is Mapped to a value

Returns the PREVIOUS Key associated value or NULL if no prior mapping exists

Page 19: 1 Sets and Maps Starring: keySet Co-Starring: Collections

19

Object get(Object key) Returns the value associated with a Key

OR NULL if no map exists or the Key does map to a NULL

Object remove (Object key)Removes the map to this Key and returns

its associated value OR returns NULL if no map existed or mapping was to NULL

Page 20: 1 Sets and Maps Starring: keySet Co-Starring: Collections

20

boolean containsKey(Object key) True if there is a key / value map

otherwise false

int size( ) Returns the number key / value mappings

Set keySet( ) Retuns the Set of keys in the map

Page 21: 1 Sets and Maps Starring: keySet Co-Starring: Collections

21

You can map:

Names to phone numbersCollege friends to the school they attend

Animals to animal soundsCoin name to its value

Car model to its makeLog in IDs to Passwords

Page 22: 1 Sets and Maps Starring: keySet Co-Starring: Collections

22

The keySet produces a Set of keys from which we can visit all of the elements of a HashMap or a TreeMap

We can visit all of the values (elements) by iterating over the key Set that is returned from the call to the Map’s keySet method

Page 23: 1 Sets and Maps Starring: keySet Co-Starring: Collections

23

The following class contains Key / Value elements

Student ID is the Key & the Name is the Value

Page 24: 1 Sets and Maps Starring: keySet Co-Starring: Collections

24

public class Student{

Integer id;String name;

public Student i, String n){

id = new Integer(i);name = n;

}public Integer getId(){

return id;}public String getName(){

return name;}public String toString(){

return id.toString() + " , " + name;}

}

Page 25: 1 Sets and Maps Starring: keySet Co-Starring: Collections

25

Illustration of a Map using the HashMap class:

Map stuffMap = new HashMap();myStuff[] ms = new Student[5];

ms[0] = new Student(21,"Farrell");ms[1] = new Student (31,"Castro");ms[2] = new Student (11,"Defazio");ms[3] = new Student (61,"Zegers");ms[4] = new Student (86,"Rogers");for (int t=0; t < 5; t++){

stuffMap.put(ms[t].getId(), ms[t]);}for (int t=0; t < 5; t++){

myStuff x = (Student)stuffMap.get(ms[t].getId());System.out.println(x.toString());

}

Page 26: 1 Sets and Maps Starring: keySet Co-Starring: Collections

26

MAP accepts "elements" as 2 separate objects, a key and the data

A MAP ADT must conform to the following:

Put Associates the specified value with the specified key in this map If the map previously contained a mapping for this key, the old value is replaced.

Page 27: 1 Sets and Maps Starring: keySet Co-Starring: Collections

27

Get -- Returns the value to which this map maps the specified key

Remove -- Removes the mapping for this key from this map if present

Page 28: 1 Sets and Maps Starring: keySet Co-Starring: Collections

28

Containskey - Returns true if this map contains a mapping for the specified key

Keyset - Returns a set view of the keys contained in this map.

Page 29: 1 Sets and Maps Starring: keySet Co-Starring: Collections

29

AP AB Subset Requirements:

Understand the requirements, restrictions and behaviors of the Set and Map ADT’s

You Will Not be required to implement the Set or Map Interface

But you WILL be required to work with the HashSet , TreeSet , HashMap & TreeMap (Discussed in Next Lecture)

Page 30: 1 Sets and Maps Starring: keySet Co-Starring: Collections

30

Project: Create Your Own Set and Map Classes

Page 31: 1 Sets and Maps Starring: keySet Co-Starring: Collections

31

NO TEST FOR THIS LECTURE !!!