This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes TreeMap Strange 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 » Java » Java in General
Bookmark "TreeMap Strange" Watch "TreeMap Strange" New topic
Author

TreeMap Strange

Jean John
Greenhorn

Joined: Dec 21, 2010
Posts: 24

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Prasanna Soundarapandiyan
Greenhorn

Joined: Sep 04, 2009
Posts: 9
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

Joined: Dec 21, 2010
Posts: 24

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
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
jQuery in Action, 2nd edition
 
subject: TreeMap Strange
 
Similar Threads
Question from John Meyer's Mock
Integer String
for each
A Map.Entry object not becoming invalid after map modification
Facing null pointer exception in static initialization