File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 13872
    
  10

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
 
jQuery in Action, 2nd edition
 
subject: Collections
 
Similar Threads
The use of Hashtable for synchronization.
HashMap or HashTable
HashMap Vs Hash table
Collections
How to determine if a List allows null elements