• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections

 
shri Sonparote
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shri Sonparote
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
shri Sonparote
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you I have one more doubt

Why set.add("sds"); throw java.lang.UnsupportedOperationException
 
Manfred Klug
Ranch Hand
Posts: 377
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic