23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In either case, we use a key as a unique identifier that is assigned by an appli cation or<br />

user to an associated value object. Thus, a map is most appropriate <strong>in</strong> situations where<br />

each key is to be viewed as a k<strong>in</strong>d of unique <strong>in</strong>dex address for its value, that is, an<br />

object that serves as a k<strong>in</strong>d of location for that value. For exam ple, if we wish to<br />

store student records, we would probably want to use student ID objects as keys (<strong>and</strong><br />

disallow two students hav<strong>in</strong>g the same student ID). In other words, the key associated<br />

with an object can be viewed as an "address" for that object. Indeed, maps are<br />

sometimes referred to as associative stores, because the key associated with an object<br />

determ<strong>in</strong>es its "location" <strong>in</strong> the data structure.<br />

The Map ADT<br />

S<strong>in</strong>ce a map stores a collection of objects, it should be viewed as a collection of<br />

key-value pairs. As an ADT, a map M supports the follow<strong>in</strong>g methods:<br />

size(): Return the number of entries <strong>in</strong> M.<br />

isEmpty(): Test whether M is empty.<br />

get(k): If Mconta<strong>in</strong>s an entry e with key equal to k, then return the<br />

value of e, else return null.<br />

put(k, v): If M does not have an entry with key equal to k, then add<br />

entry (k, v) to M <strong>and</strong> return null; else, replace with v the exist<strong>in</strong>g value of the<br />

entry with key equal to k <strong>and</strong> return the old value.<br />

517

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!