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 9204185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:38:58+00:00 2026-06-17T23:38:58+00:00

How come I’m allowed to do this: TreeSet<Object> treeSet = new TreeSet<Object>(); treeSet.add(new Object());

  • 0

How come I’m allowed to do this:

TreeSet<Object> treeSet = new TreeSet<Object>();
treeSet.add(new Object());

But not this:

final List<Object> objects = new ArrayList<Object>();
Collections.sort(objects);

The first one gives me an ClassCastException but the second one gives me a compile error. As I understand it the actual problem is the same in both cases: java.lang.Object does not implement the Comparable interface.

UPDATE:
Hmm for some reason this only applies for Java 7 and not 6.
Am I being stupid or tired? Could someone please shed some light on this?

UPDATE #2:
I do get different results depending on java versions. Please see picture:
enter image description here

  • 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-06-17T23:39:00+00:00Added an answer on June 17, 2026 at 11:39 pm

    Collections.sort requires a list of Comparable‘s. TreeSet doesn’t have that restriction in the typing. With the default constructor, the docs say it sorts by ” natural ordering of its elements”. What isn’t clear is how it would sort Object, but your problem deals with the typing.

    Update:
    I missed the last part of the question. Without looking at the stacktrace, I would guess that since the default constructor for TreeSet tries to sort by “natural order”, internally, it is doing a cast to Comparable, which would cause a ClassCastException.

    Update:
    I looked closer at the javadocs for TreeSet (JDK 6, JDK 7) and it says

    Constructs a new, empty tree set, sorted according to the natural
    ordering of its elements. All elements inserted into the set must
    implement the Comparable interface. Furthermore, all such elements
    must be mutually comparable: e1.compareTo(e2) must not throw a
    ClassCastException for any elements e1 and e2 in the set. If the user
    attempts to add an element to the set that violates this constraint
    (for example, the user attempts to add a string element to a set whose
    elements are integers), the add call will throw a ClassCastException.

    for both. So, if no ClassCastException happens with JDK6, perhaps it is a bug.

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

Sidebar

Related Questions

How come I can get the result from the Från omgång list but not
I come from the Java world, so to me it's all object.foo() , but
I come across this code in my web.xml but I don't understand what it
How come the following does not work on ios6, but replacing translate with something_else
How come this program does not read a hex file correctly? #include <stdio.h> #include
I come from a world of c# were doing something like this is allowed.
How come this doesn't work (operating on an empty select list <select id=requestTypes></select> $(function()
Come across what looks at first sight like an MT-issue, but I'm trying to
I come from ruby/C# and am new to Python. I'm looking at the following
How come this works: import datetime now = datetime.datetime.now() month = '%d' % now.month

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.