aspose 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 Spring in Action this week in the Spring 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: 14269
    
  21

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 8 API documentation
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