• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dan's ? from Collections.

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic