66
Map 7 January 2019 OSU CSE 1

CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Map

7 January 2019 OSU CSE 1

Page 2: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Map

• The Map component family allows you to manipulate mappings from keys (of any type K) to values (of any type V)– A Map variable holds a very simple “database”

of keys and their associated values– Example: If you need to keep track of the

exam grade for each student, you might use a Map<String,Integer> variable

7 January 2019 OSU CSE 2

Page 3: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Interfaces and Classes

7 January 2019 OSU CSE 3

Map1L

implements implements

Map2 Map3

Map

MapKernel

extends

Standard

extends

Iterable

extends

Page 4: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Interfaces and Classes

7 January 2019 OSU CSE 4

Map1L

implements implements

Map2 Map3

Map

MapKernel

extends

Standard

extends

Iterable

extendsStandard has contracts

for three methods:clear

newInstancetransferFrom

Page 5: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Interfaces and Classes

7 January 2019 OSU CSE 5

Map1L

implements implements

Map2 Map3

Map

MapKernel

extends

Standard

extends

Iterable

extends

MapKernel has contracts for six methods:

addremove

removeAnyvaluehasKeysize

Page 6: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Interfaces and Classes

7 January 2019 OSU CSE 6

Map1L

implements implements

Map2 Map3

Map

MapKernel

extends

Standard

extends

Iterable

extends

Maphas contracts for five

other methods:replaceValue

keyhasValue

sharesKeyWithcombineWith

Page 7: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Interfaces and Classes

7 January 2019 OSU CSE 7

Map1L

implements implements

Map2 Map3

Map

MapKernel

extends

Standard

extends

Iterable

extends

Iterable has a contract for one method:iterator

Page 8: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Mathematical Model

• The value of a Map variable is modeled as a finite set of ordered pairs of type (K, V) with “the function property”, i.e., no two pairs in the set have the same K value– This is sometimes called a (finite) partial

function from K to V

7 January 2019 OSU CSE 8

Page 9: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Partial FunctionPARTIAL_FUNCTION is finite set of

(key: K, value: V)

exemplar m

constraintfor all key1, key2: K, value1, value2: V

where ((key1, value1) is in m and(key2, value2) is in m)

(if key1 = key2 then value1 = value2)

7 January 2019 OSU CSE 9

Page 10: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Partial FunctionPARTIAL_FUNCTION is finite set of

(key: K, value: V)

exemplar m

constraintfor all key1, key2: K, value1, value2: V

where ((key1, value1) is in m and(key2, value2) is in m)

(if key1 = key2 then value1 = value2)

7 January 2019 OSU CSE 10

This formally states “the function property” for a set of

ordered pairs.

Page 11: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Domain of a (Partial) FunctionDOMAIN (

m: PARTIAL_FUNCTION

): finite set of K

satisfiesfor all key: K(key is in DOMAIN(m) iffthere exists value: V((key, value) is in m))

7 January 2019 OSU CSE 11

Page 12: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Range of a (Partial) FunctionRANGE (

m: PARTIAL_FUNCTION

): finite set of V

satisfiesfor all value: V(value is in RANGE(m) iffthere exists key: K((key, value) is in m))

7 January 2019 OSU CSE 12

Page 13: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Mathematical Model

• Formally:type Map is modeled byPARTIAL_FUNCTION

7 January 2019 OSU CSE 13

Page 14: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

No-argument Constructor

• Ensures:this = { }

7 January 2019 OSU CSE 14

Page 15: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 15

Code State

Map<String,Integer> m =new Map1L<>();

Page 16: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 16

Code State

Map<String,Integer> m =new Map1L<>();

m = { }

Page 17: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

add

void add(K key, V value)

• Adds the pair (key, value) to this.• Aliases: references key, value• Updates: this• Requires:key is not in DOMAIN(this)

• Ensures:this = #this union {(key, value)}

7 January 2019 OSU CSE 17

Page 18: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 18

Code Statem = {("PB", 99),

("BW", 17)} k = "PS"v = 99

m.add(k, v);

Page 19: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 19

Code Statem = {("PB", 99),

("BW", 17)} k = "PS"v = 99

m.add(k, v);

Is the requires clause satisfied? What isDOMAIN(m)?

Page 20: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 20

Code Statem = {("PB", 99),

("BW", 17)} k = "PS"v = 99

m.add(k, v);

m = {("PB", 99),("BW", 17),("PS", 99)}

k = "PS"v = 99

Page 21: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 21

Code Statem = {("PB", 99),

("BW", 17)} k = "PS"v = 99

m.add(k, v);

m = {("PB", 99),("BW", 17),("PS", 99)}

k = "PS"v = 99

Note the aliases created here, which you cannot see in the tracing table; you should be able to draw the appropriate

diagram showing them.

Page 22: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Another Interface

• The Map interface includes an interface for another related generic type, Map.Pair

• Its mathematical model is simply an ordered pair of a key and a value

• Formally:type Map.Pair is modeled by(key: K, value: V)

7 January 2019 OSU CSE 22

Page 23: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Map.Pair Methods

• This (immutable) type has only a constructor (taking a K and a V) and a getter method for each pair component– K key()

• Returns the first component of this• Aliases: reference returned by key

– V value()• Returns the second component of this• Aliases: reference returned by value

7 January 2019 OSU CSE 23

Page 24: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

removeMap.Pair<K,V> remove(K key)

• Removes from this the pair whose first component is key and returns it.

• Updates: this• Requires:

key is in DOMAIN(this)

• Ensures:remove.key = key andremove is in #this andthis = #this \ {remove}

7 January 2019 OSU CSE 24

Page 25: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 25

Code Statem = {("PB", 99),

("BW", 17)} k = "BW"

Map.Pair<String,Integer> p =m.remove(k);

Page 26: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 26

Code Statem = {("PB", 99),

("BW", 17)} k = "BW"

Map.Pair<String,Integer> p =m.remove(k);

m = {("PB", 99)} k = "BW"p = ("BW", 17)

Page 27: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

removeAny

Map.Pair<K,V> removeAny()

• Removes and returns an arbitrary pair from this.

• Updates: this• Requires:

|this| > 0

• Ensures:removeAny is in #this andthis = #this \ {removeAny}

7 January 2019 OSU CSE 27

Page 28: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 28

Code Statem = {("PB", 99),

("BW", 17),("PS", 99)}

Map.Pair<String,Integer> p =m.removeAny();

Page 29: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 29

Code Statem = {("PB", 99),

("BW", 17),("PS", 99)}

Map.Pair<String,Integer> p =m.removeAny();

m = {("PB", 99),("BW", 17)}

p = ("PS", 99)

Page 30: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

value

V value(K key)

• Reports the value associated with key in this.

• Aliases: reference returned by value• Requires:key is in DOMAIN(this)

• Ensures:(key, value) is in this

7 January 2019 OSU CSE 30

Page 31: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 31

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"v = -423

v = m.value(k);

Page 32: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 32

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"v = -423

v = m.value(k);

m = {("PB", 99),("BW", 17)}

k = "PB"v = 99

Page 33: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 33

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"v = -423

v = m.value(k);

m = {("PB", 99),("BW", 17)}

k = "PB"v = 99

Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.

Page 34: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

hasKey

boolean hasKey(K key)

• Reports whether there is a pair in thiswhose first component is key.

• Ensures:hasKey =

(key is in DOMAIN(this))

7 January 2019 OSU CSE 34

Page 35: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 35

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"

boolean b =m.hasKey(k);

Page 36: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 36

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"

boolean b =m.hasKey(k);

m = {("PB", 99),("BW", 17)}

k = "PB"b = true

Page 37: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

size

int size()

• Reports the size (cardinality) of this.• Ensures:size = |this|

7 January 2019 OSU CSE 37

Page 38: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

replaceValueV replaceValue(K key, V value)• Replaces the value associated with key in this by value, and returns the old value.

• Aliases: reference value• Updates: this• Requires:

key is in DOMAIN(this)

• Ensures:this = (#this \ {(key, replaceValue)})

union {(key, value)} and(key, replaceValue) is in #this

7 January 2019 OSU CSE 38

Page 39: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 39

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"v = 85

Integer oldV =m.replaceValue(k, v);

Page 40: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 40

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"v = 85

Integer oldV =m.replaceValue(k, v);

m = {("PB", 85),("BW", 17)}

k = "PB"v = 85oldV = 99

Page 41: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 41

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"v = 85

Integer oldV =m.replaceValue(k, v);

m = {("PB", 85),("BW", 17)}

k = "PB"v = 85oldV = 99

Note the alias created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it.

Page 42: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Another Example

7 January 2019 OSU CSE 42

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"v = 85

v = m.replaceValue(k, v);

Page 43: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Another Example

7 January 2019 OSU CSE 43

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"v = 85

v = m.replaceValue(k, v);

m = {("PB", 85),("BW", 17)}

k = "PB"v = 99

Page 44: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Another Example

7 January 2019 OSU CSE 44

Code Statem = {("PB", 99),

("BW", 17)} k = "PB"v = 85

v = m.replaceValue(k, v);

m = {("PB", 85),("BW", 17)}

k = "PB"v = 99

This use of the method avoids creating an alias: it swaps v with the value in m that was previously

associated with k.

Page 45: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

key

K key(V value)

• Reports some key associated with value in this.

• Aliases: reference returned by key• Requires:value is in RANGE(this)

• Ensures:(key, value) is in this

7 January 2019 OSU CSE 45

Page 46: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 46

Code Statem = {("PB", 99),

("BW", 17)} k = "xyz"v = 99

k = m.key(v);

Page 47: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 47

Code Statem = {("PB", 99),

("BW", 17)} k = "xyz"v = 99

k = m.key(v);

m = {("PB", 99),("BW", 17)}

k = "PB"v = 99

Page 48: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 48

Code Statem = {("PB", 99),

("BW", 17)} k = "xyz"v = 99

k = m.key(v);

m = {("PB", 99),("BW", 17)}

k = "PB"v = 99

The method value is part of the intended use of a Map and is efficient in most classes that

implement Map; the method key is rarely of interest and is inefficient in most classes that implement Map.

Page 49: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

hasValue

boolean hasValue(V value)

• Reports whether there is a pair in thiswhose second component is value.

• Ensures:hasValue =

(value is in RANGE(this))

7 January 2019 OSU CSE 49

Page 50: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 50

Code Statem = {("PB", 99),

("BW", 17)} v = 17

boolean b =m.hasValue(v);

Page 51: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 51

Code Statem = {("PB", 99),

("BW", 17)} v = 17

boolean b =m.hasValue(v);

m = {("PB", 99),("BW", 17)}

v = 17b = true

Page 52: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 52

Code Statem = {("PB", 99),

("BW", 17)} v = 17

boolean b =m.hasValue(v);

m = {("PB", 99),("BW", 17)}

v = 17b = true

The method hasKey is part of the intended use of a Map and is efficient in most classes that implement Map; the method

hasValue is rarely of interest and is inefficient in most classes that

implement Map.

Page 53: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

combineWith

void combineWith(Map<K,V> m)• Combines m with this.• Updates: this• Clears: m• Requires:DOMAIN(this) intersection

DOMAIN(m) = {}

• Ensures:this = #this union #m

7 January 2019 OSU CSE 53

Page 54: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 54

Code Statem1 = {("PB", 99),

("BW", 17)} m2 = {("PS", 99)}

m1.combineWith(m2);

Page 55: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 55

Code Statem1 = {("PB", 99),

("BW", 17)} m2 = {("PS", 99)}

m1.combineWith(m2);

m1 = {("PB", 99),("BW", 17),("PS", 99)}

m2 = { }

Page 56: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

sharesKeyWith

boolean sharesKeyWith(Map<K,V> m)

• Reports whether this and m have any keys in common.

• Ensures:sharesKeyWith=

(DOMAIN(this) intersectionDOMAIN(m) /= {})

7 January 2019 OSU CSE 56

Page 57: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 57

Code Statem1 = {("PB", 99),

("BW", 17)} m2 = {("PS", 99)}

boolean b =m1.sharesKeyWith(m2);

Page 58: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

7 January 2019 OSU CSE 58

Code Statem1 = {("PB", 99),

("BW", 17)} m2 = {("PS", 99)}

boolean b =m1.sharesKeyWith(m2);

m1 = {("PB", 99),("BW", 17)}

m2 = {("PS", 99)}b = false

Page 59: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

iterator

Iterator<Map.Pair<K,V>> iterator()

• Returns an iterator over a set of elements of type Map.Pair<K,V>.

• Ensures:entries(~this.seen * ~this.unseen) = this

and|~this.seen * ~this.unseen| = |this|

7 January 2019 OSU CSE 59

Page 60: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Example

• Suppose you have a Map that keeps track of the names and associated salaries of all employees in the company:Map<String,NaturalNumber> m =

new Map1L<>();

...

7 January 2019 OSU CSE 60

Page 61: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Sample For-Each Loop: Danger!

• Here’s how you might try to give every employee a $10,000 raise:

NaturalNumber raise =

new NaturalNumber2(10000);

for (Map.Pair<String,NaturalNumber> p : m) {

NaturalNumber salary = p.value();

salary.add(raise);

}

7 January 2019 OSU CSE 61

Page 62: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Sample For-Each Loop: Danger!

• Here’s how you might try to give every employee a $10,000 raise:

NaturalNumber raise =

new NaturalNumber2(10000);

for (Map.Pair<String,NaturalNumber> p : m) {

NaturalNumber salary = p.value();

salary.add(raise);

}

7 January 2019 OSU CSE 62

Draw this diagram: p holds aliases to some key and its associated value in m; the method value returns an alias to a NaturalNumber that is also in the Map m;

so, changing that NaturalNumber incidentally changes the values of both p and m (even though no

Map method is called in the loop).

Page 63: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Sample For-Each Loop: Danger!

• Here’s how you might try to give every employee a $10,000 raise:

NaturalNumber raise =

new NaturalNumber2(10000);

for (Map.Pair<String,NaturalNumber> p : m) {

NaturalNumber salary = p.value();

salary.add(raise);

}

7 January 2019 OSU CSE 63

Danger!This violates the rules for using

iterators and for-each loops!

Page 64: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

The Safe Way• Here’s how you should give every

employee a $10,000 raise:NaturalNumber raise = new NaturalNumber2(10000);

Map<String, NaturalNumber> temp = m.newInstance();

temp.transferFrom(m);

while (temp.size() > 0) {

Map.Pair<String, NaturalNumber> p =

temp.removeAny();

p.value().add(raise);

m.add(p.key(), p.value());

}

7 January 2019 OSU CSE 64

Page 65: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

The Safe Way• Here’s how you should give every

employee a $10,000 raise:NaturalNumber raise = new NaturalNumber2(10000);

Map<String, NaturalNumber> temp = m.newInstance();

temp.transferFrom(m);

while (temp.size() > 0) {

Map.Pair<String, NaturalNumber> p =

temp.removeAny();

p.value().add(raise);

m.add(p.key(), p.value());

}

7 January 2019 OSU CSE 65

Draw this diagram: p holds references to some key and its associated value, but now they are not in any Map and p is not in any Map; the method value

returns an alias to a NaturalNumber in the Map.Pair p; so, changing that NaturalNumber

does not incidentally change the value of m or temp(even though that actually would be OK for this loop).

Page 66: CSE 2221 - Mapweb.cse.ohio-state.edu/software/2221/web-sw1/extras/slides/34.Map.pdf · Map • The . Map. component family allows you to manipulate . mappings. from . keys (of any

Resources

• OSU CSE Components API: Map– http://cse.osu.edu/software/common/doc/

7 January 2019 OSU CSE 66