jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable" Watch "java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable" New topic
Author

java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234



Hi , when i am executing this piece of code above i am getting this exception ,



please let me know why this exception is coming in this case .

Thanks in advnace .


Save India From Corruption - Anna Hazare.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Your Employee class needs to implement the Comparable interface or you need to provide a Comparator to the TreeSet.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks Wouter , its working after my DTO implementing the Comparable interface ??

But why this is required only for TreeSet class ?? adding more confusion to me
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The TreeSet orders it's elements. To do that it either needs a Comparator or if you don't provide one the element-class must implement the Comparable interface. You didn't provide a Comparator so the TreeSet casted the element to a comparable and that caused the Exception because your class didn't implement it.

If you're still confused read this.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


Till today i was adding Strings so didn't realized that , but when i added Custom Object the concept was clear .

Thanks Wouter, it was nice explanation .
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Glad I could help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Edit: reply to Ravi's question why it was only required for TreeSet

It isn't. You would have had the same error when using these objects in a TreeMap, or when they were in a List and you sorted it using Collections.sort(List). Actually, the latter would have given a compiler error since Java 5.0, but before it would have given the error.

Some structures or methods simply require Comparables. TreeSet and TreeMap use sorting for their internal storage. If you don't provide a Comparator then TreeSet / TreeMap assumes that the elements implement Comparable (see the Javadoc).

Collections.sort(List) has been able to prevent this using generics, but TreeSet / TreeMap couldn't. Removing any of the constructors was not an option as it would break older code. Making the generic element / key type bound on Comparable was also not an option because then it wouldn't be possible to create a TreeSet / TreeMap with non-Comparable objects using a Comparator.
If TreeSet / TreeMap could be changed completely, they should have no public constructors but factory methods instead (the constructors become private):
Apart from not being able to create a TreeSet without a Comparator, it would also give type inference:

Now about these two comments in my code. SortedSet / SortedMap are interfaces. That allows me to technically create a SortedSet implementing class which uses the system hash code for ordering. That would not require a comparator (so comparator() returns null) but the elements are still not Comparable. That would still be a possible type safety problem waiting to happen.

Hmmm. Perhaps my story / rant is a bit over complex for this discussion. Ah well


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3605
    
  14

You should also take care that you parameterize your generic types correctly. For instance, don't use:Use this instead:This has nothing to do with your problem, but it's good practice to get used to.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


Some structures or methods simply require Comparables. TreeSet and TreeMap use sorting for their internal storage.


Thanks Rob for the above . and strphan for your valuable suggestions .

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable
 
Similar Threads
ClassCastException
Employee with same values is being added two times in HashSet
Overloading (and NOT overriding) equals() method
Question 10 Chapter 7 : K and B
A question About overriding equals and hashcode for an Object