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. .
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 key.as 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 !!!
Joined: Dec 04, 2002
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.
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. Thanks Sri
Prashant, 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="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>