This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Does TreeSet allows only comparable objects - class cast exception

 
Ranch Hand
Posts: 57
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

i would like to add Comparator objects to TreeSet, while doing ClassCastException throwing. Could you please help on this.

My question is when I implement my custom class with comparable every this goes fine. But what is the way to add Comparator objects to Treeset?








Exception in thread "main" java.lang.ClassCastException: com.exceptions.Person cannot be cast to java.lang.Comparable
 
Sheriff
Posts: 15733
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the JavaDocs for TreeSet. There's a constructor that takes a Comparator.
 
Marshal
Posts: 69748
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have read the documentation for TreeSet; you must either pass an object implementing Comparable<T> or you must pass a Comparator<T> as a separate argument (I think to the constructor).

No, that is not the right way to use a Comparator. Create it in a different class, as an anonymous class, or as a λ. You might do well to change your class to implement the Comparable<T> interface, which is probably the easiest solution at present.
Why is your class called person if it has a salary field and an id field? Surely you have written an Employee class there?
 
Junilu Lacar
Sheriff
Posts: 15733
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, it makes no sense for Person to implement Comparator<Person>. Comparators should be separate entities because they are like a third party arbiter, taking two Person objects and evaluating how they compare to each other. It may make sense in the real world that every Person could potentially serve in that capacity (that's why we have judges and juries, after all) but in an object-oriented programming context, it doesn't. That's just going to be confusing. Look for examples of how to write a Comparator
 
Campbell Ritchie
Marshal
Posts: 69748
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kiran nyala wrote:. . . com.exceptions.Person cannot be cast to java.lang.Comparable

Don't use that package name, unless you own the www.exceptions.com website. More details in the Java™ Tutorials.
 
kiran nyala
Ranch Hand
Posts: 57
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your response.

I did with TreeSet constructor with comparator. thank you
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic