Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Collections.sort</code> requires a list of <code>Comparable</code>'s. <code>TreeSet</code> 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 <code>Object</code>, but your problem deals with the typing.</p> <p>Update: I missed the last part of the question. Without looking at the stacktrace, I would guess that since the default constructor for <code>TreeSet</code> tries to sort by "natural order", internally, it is doing a cast to <code>Comparable</code>, which would cause a <code>ClassCastException</code>.</p> <p>Update: I looked closer at the javadocs for <code>TreeSet</code> (<a href="http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html#TreeSet%28%29" rel="nofollow">JDK 6</a>, <a href="http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#TreeSet%28%29" rel="nofollow">JDK 7</a>) and it says</p> <blockquote> <p>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.</p> </blockquote> <p>for both. So, if no ClassCastException happens with JDK6, perhaps it is a bug.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload