Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan's ? from Collections.

 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
.
 
Chetan M
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic