wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections" Watch "Collections" New topic
Author

Collections

shri Sonparote
Ranch Hand

Joined: May 07, 2007
Posts: 30
Hi all
Please Solve my query about HashMap and TreeMap.
We Can Store null key in HashMap but we can't store
null key in TreeMap It is giving runtime exception.
Why this is so?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

TreeMap sorts its elements by key. The map cannot be sorted properly if one of the keys is 'null' - so 'null' is not allowed as a key.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
shri Sonparote
Ranch Hand

Joined: May 07, 2007
Posts: 30
Thank you
shri Sonparote
Ranch Hand

Joined: May 07, 2007
Posts: 30
Thank you I have one more doubt

Why set.add("sds"); throw java.lang.UnsupportedOperationException
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by shri Sonparote:
Why set.add("sds"); throw java.lang.UnsupportedOperationException


Have a look at the API documentation of TreeMap.keySet().
shri Sonparote
Ranch Hand

Joined: May 07, 2007
Posts: 30
Thank You
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections
 
Similar Threads
How to determine if a List allows null elements
HashMap or HashTable
HashMap Vs Hash table
The use of Hashtable for synchronization.
Collections