Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 687953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:10:48+00:00 2026-05-14T02:10:48+00:00

List<MyClass> myclassList = (List<MyClass>) rs.get(); TreeSet<MyClass> myclassSet = new TreeSet<MyClass>(myclassList); I don’t understand why

  • 0
List<MyClass> myclassList = (List<MyClass>) rs.get();

TreeSet<MyClass> myclassSet = new TreeSet<MyClass>(myclassList);

I don’t understand why this code generates this:

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

MyClass does not implement Comparable. I just want to use a Set to filter the unique elements of the List since my List contains unncessary duplicates.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:10:48+00:00Added an answer on May 14, 2026 at 2:10 am

    Does MyClass implements Comparable<MyClass> or anything like that?

    If not, then that’s why.

    For TreeSet, you either have to make the elements Comparable, or provide a Comparator. Otherwise TreeSet can’t function since it wouldn’t know how to order the elements.

    Remember, TreeMap implements SortedSet, so it has to know how to order
    the elements one way or another.

    You should familiarize yourself with how implementing Comparable
    defines natural ordering for objects of a given type.

    The interface defines one method, compareTo, that must return a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than the other object respectively.

    The contract requires that:

    • sgn(x.compareTo(y)) == -sgn(y.compareTo(x))
    • it’s transitive: x.compareTo(y)>0 && y.compareTo(z)>0 implies x.compareTo(z)>0
    • x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)) for all z

    Additionally, it recommends that:

    • (x.compareTo(y)==0) == (x.equals(y)), i.e. “consistent with equals

    This may seem like much to digest at first, but really it’s quite natural with
    how one defines total ordering.


    If your objects can not be ordered one way or another, then a TreeSet wouldn’t make sense. You may want to use a HashSet instead, which have its own contracts. You are likely to be required to @Override hashCode() and equals(Object) as appropriate for your type (see: Overriding equals and hashCode in Java)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

List<MyClass> MyClassPro { get;set; } MyClass obj = new MyClass(); obj.MyClassPro = null; Consider
I have a class like this: public class myClass { public List<myOtherClass> anewlist =
For example: List<MyClass> myList = new List<MyClass>(); ... // add lots of members... ...
I have found this example on StackOverflow: var people = new List<Person> { new
I have a simple class like this: public class myClass { public List<string> innerList;
I'd like something like int minIndex = list.FindMin(delegate (MyClass a, MyClass b) {returns a.CompareTo(b);});
MyClass[] array; List<MyClass> list; What are the scenarios when one is preferable over the
I'm de/serializing an object like so: public class myClass : ISerializable { public List<OType>
List Comprehension is a very useful code mechanism that is found in several languages,
List<String> nameList = new List<String>(); DropDownList ddl = new DropDownList(); List is populated here,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.