File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's ? from Collections. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic

Dan's ? from Collections.

Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
Hi All,
I came across a question from Dan's Collection chapters. It goes like this..
a. Stores key/value pairs.
b. Allows null elements, keys, and values.
c. Duplicate entries replace old entries.
d. Entries are sorted using a comparator or the
Comparable interface.

Which of these classes provides the specified features?
a. LinkedList
b. TreeMap
c. TreeSet
d. HashMap
e. HashSet
f. Hashtable
g. None of the above
Ans given is b.But is it satisfy b condition.?Thais is
Allows null elements, keys, and values.

--------------<br />Prashant<br />SCJP-91%
Chetan M
Ranch Hand

Joined: Dec 18, 2002
Posts: 43
Hi there,
The answer specified is correct as for as my knowledge.
When he says allow null elememnts he means...
Treemap can take null for values of any key.. and not null for well as value, As any kep in a map can't be null.
"TreeMap is the only class where u get key value map with sorted keys and duplicate entries are replaced"

The More You Share , The More You Learn

What Matters Most Is How You See Your Self.

No matter how much you know today, you will have to know more tomorrow.<br /> Hakunamatata !!!
Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
yeah!!! may be he is talking about null elements(or values ) but not keys.
chetan as u made statement any key in a map can't be null is false.Coz, HashMap can take null value as key.
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
If my understanding of Map and its subclasses is right,
Maps(HashMap,LinkedHashMap,TreeMap) can allow at the most one null key and any number of null values. But Hashtable is an exception in the sense that it does not allow null values or keys.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
If the TreeMap is constructed without a Comparator then every key must implement the Comparable interface. Since null can not implement Comparable a TreeMap constructed without a Comparator can not accept null keys. However, a Comparator can accept null values so a TreeMap constructed with a Comparator can accept null keys if the Comparator accepts null values.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
I agree. Here's the link:
subject: Dan's ? from Collections.
jQuery in Action, 3rd edition