File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeMap Strange

 
Jean John
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is that if your treemap is empty and you try to remove some invalid key,there is no exception, whereas if the treemap has atleast one element and you try to remove an invalid key,it is a runtime exception? someone please explain.. Thanks.

This is the scenario i am talking about



output:null
(No error)







output: Run time Exception (Only for TreeMap
LinkedHashMap and HashMap dont care about what you are trying to remove)
 
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
Welcome to JavaRanch!

The error that you get does not have anything to do with whether the map is empty or not. You're using it the wrong way.

You have a Map<Integer, String>. When you call the remove() method, you must call it with an Integer as the argument, but you are calling it with a string. Instead of this:

you should be doing something like this:

 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags next time; I've added them for you this time.

Jesper, I (partly) disagree with your answer. Map.remove explicitly uses Object as its argument type; you are allowed to try to remove anything from the map. There's a but there though; the method is allowed to throw a ClassCastException.

Jean, the reason for this exception is this: TreeMap uses the compareTo method of its elements to find elements*. This means that when you try to remove the String, it will compare the String to the Integer. That's not allowed though; as String.compareTo requires a String and Integer.compareTo requires an Integer. I don't know which is being compared to which, but in the end the comparison is causing the ClassCastException. And unfortunately, that's allowed under the contract of Map. I get the feeling this exception was added because of TreeMap though.

As for why HashMap and LinkedHashMap don't throw an exception: these use hashCode() and equals. Unlike compareTo, equals is not allowed to throw a ClassCastException when the passed argument is of a wrong class; it's required to return false instead. So "hi".compareTo(1) throws an exception, "hi".equals(1) returns false.


* unless a Comparator is given in the constructor; in that case, the Comparator's compare method is used.
 
Prasanna Soundarapandiyan
Greenhorn
Posts: 9
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, its really a nice question..

The reason for this behaviour is the implementation of TreeMap. while trying to remove an object out of Treemap, you are calling map.remove(key) method. Please see the implementation of remove method for Treemap,



So, if you dont have any object in your map, then its size will be 0, and obviously it will return null only with out allowing further processing.
 
Jean John
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Soundarapandiyan wrote:Well, its really a nice question..

The reason for this behaviour is the implementation of TreeMap. while trying to remove an object out of Treemap, you are calling map.remove(key) method. Please see the implementation of remove method for Treemap,



So, if you dont have any object in your map, then its size will be 0, and obviously it will return null only with out allowing further processing.


Seeing the implementation was really helpful..Thank you... Can you please tell me where you get the implementation details of the various java methods. Thanks in advance
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jean John wrote:
Seeing the implementation was really helpful..Thank you... Can you please tell me where you get the implementation details of the various java methods. Thanks in advance


The source code for most of the core library is provided with the JDK -- as a src zip file on the top level directory of the installation.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic