• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TreeMap Strange

 
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic