• 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

Does TreeSet allows only comparable objects - class cast exception

 
Ranch Hand
Posts: 57
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the JavaDocs for TreeSet. There's a constructor that takes a Comparator.
 
Marshal
Posts: 79281
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79281
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your response.

I did with TreeSet constructor with comparator. thank you
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic