• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt pertaining to compare() in TreeSet  RSS feed

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In TreeSet if we put objects that are not mutually comparable then ClassCastException occurs.
And if we put user defined objects in TreeSet without overriding the compareTo() in our user defined object
then also classCastException occurs.
Actually why ClassCastException occurs??
what happens inside compare() in TreeSet ot TreeMap I mean why they have done casting in compare()??
If possible please write psuedo code describing what goes on inside compare() of TreeMap.






thanks in advance
with regards
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A TreeSet is a sorted collection. That means the objects that you put in a TreeSet have to be mutually comparable ( english ) or the TreeSet won't know how to compare them to keep them sorted.

If an object is Comparable ( Java )- it's an instance of a type (class) that implements Comparable -- meaning the logic to compare its two instances is provided by the compareTo method -- then the TreeSet can use that logic to compare the objects to keep them sorted. This comparison scheme creates a sort order which is commonly referred to as the natural sort order for that type.

If an object is not Comparable ( Java ) or if an object is Comparable ( Java ) but you want the TreeSet to use a different comparison logic than the default/natural comparison scheme ( through the compareTo method ) then you can use the Comparator interface to create a custom comparison logic and pass it to the TreeSet constructor so it will use that logic to compare two objects and sort the objects accordingly.

You get a ClassCastException cause the objects that are put in a TreeSet have to be mutually Comparable. If a class has not implemented Comparable and you add its instances to a TreeSet ( that was constructed without providing a custom comparison scheme through a Comparator ), the TreeSet tries to cast them to Comparable and this step fails and hence you get the ClassCastException.
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, it seems that your question only concerns TreeSet and TreeMap instances without custom comparator. It is possible to provide a custom comparator when creating these instances, and the inner mechanics would then be slightly different. So for now, I'll concentrate only on the cases where you don't use a custom comparator.

There can be two kinds of problems in this scenario: firstly, you might try to insert an object of a class that doesn't implement the Comparable interface. Proper use of generics should prevent you from doing so, but it is possible to work-around the compiler's checks, most usually by using raw types. When this happens, the ClassCastException occurs as soon as the collection casts the object being inserted to Comparable to be able to call its compareTo() method - it has to cast, since it couldn't call its compareTo() method otherwise, hence the exception.

Another problem might arise when you try to insert objects that do implement Comparable, but are not mutually comparable, say a String and an Integer. Again, proper use of generics should prevent this, but people get sometimes creative. These classes do implement the Comparable interface, but with different signature (in our case, one takes a String as a parameter, while the other takes an Integer). The ClassCastException therefore occurs as soon as the collection tries to call the method using incompatible types.

Yet another possibility is that the compareTo() method itself can throw the ClassCastException. This is completely outside of the control of the collection.

In any case, I'd suggest reading the Oracle's Object Ordering tutorial.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RabiDas Sharma wrote:Actually why ClassCastException occurs??

I think Chan and Martin have covered pretty much everything, but my question to you: What would you expect it to do?

'Tree' collections require that elements are mutually Comparable, but the type you supply is simply an <E> (or, in the case of a TreeMap, a <K>).

So, if you don't supply a Comparator, something, somewhere has to make sure that the elements it's comparing are Comparable - and what better way to do that than to cast them?

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!