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.

emove(k): Remove from M the entry with key equal to k, <strong>and</strong> return<br />

its value; if M has no such entry, then return null.<br />

keys(): Return an iterable collection conta<strong>in</strong><strong>in</strong>g all the keys stored <strong>in</strong><br />

M (so keys().iterator() returns an iterator of keys).<br />

values(): Return an iterable collection conta<strong>in</strong><strong>in</strong>g all the values as<br />

sociated with keys stored <strong>in</strong> M (so values().iterator() re turns an iterator<br />

of values).<br />

entries(): Return an iterable collection conta<strong>in</strong><strong>in</strong>g all the key-value<br />

entries <strong>in</strong> M (so entries().iterator() returns an iterator of entries).<br />

When operations get(k), put(k,v) <strong>and</strong> remove(k) are performed on a map M that<br />

has no entry with key equal to k, we use the convention of return<strong>in</strong>g null. A special<br />

value such as this is known as a sent<strong>in</strong>el. (See also Section 3.3.) The disadvantage<br />

with us<strong>in</strong>g null as such a sent<strong>in</strong>el is that this choice can create ambiguity should we<br />

every want to have an entry (k, null) with value null <strong>in</strong> the map. Another choice, of<br />

course, would be to throw an exception when someone requests a key that is not <strong>in</strong><br />

our map. This would probably not be an appropriate use of an exception, however,<br />

s<strong>in</strong>ce it is normal to ask for someth<strong>in</strong>g that might not be <strong>in</strong> our map. Moreover,<br />

throw<strong>in</strong>g <strong>and</strong> catch<strong>in</strong>g an exception is typically slower than a test aga<strong>in</strong>st a sent<strong>in</strong>el;<br />

hence, us<strong>in</strong>g a sent<strong>in</strong>el is more efficient (<strong>and</strong>, <strong>in</strong> this case, conceptually more<br />

appropriate). So we use null as a sent<strong>in</strong>el for a value associated with a miss<strong>in</strong>g key.<br />

Example 9.1: In the follow<strong>in</strong>g, we show the effect of a series of operations on<br />

an <strong>in</strong>itially empty map stor<strong>in</strong>g entries with <strong>in</strong>teger keys <strong>and</strong> s<strong>in</strong>gle-character values.<br />

Operation<br />

Output<br />

Map<br />

isEmpty()<br />

true<br />

φ<br />

put(5,A)<br />

null<br />

{(5,A)}<br />

put(7,B)<br />

518

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

Saved successfully!

Ooh no, something went wrong!