Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 .
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Employee class needs to implement the Comparable interface or you need to provide a Comparator to the TreeSet.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Pie
Posts: 5415
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 .

 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic