Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What Comparators are defaulted?

843793Apr 4 2003 — edited Apr 4 2003
Using the JSR14 compiler I'm getting a crash that seems non-intuitive to me as to the cause. I've written a segment of code that simulates the same effect:
public class MapTest{
    public static void main( String[] args ){
        Set<Outer> oset = new TreeSet<Outer>();
        for( int i = 0; i < 5; ++i ){
            System.out.println( i );
            Inner j = new Inner();
            oset.add( j );
        }
    }
}
class Outer{ public int x; }
class Inner extends Outer{ public int y; }
which crashes when inserting the second element with a ClassCastException:
Exception in thread "main" java.lang.ClassCastException
at java.util.TreeMap.compare(TreeMap.java:1081)
at java.util.TreeMap.put(TreeMap.java:459)
at java.util.TreeSet.add(TreeSet.java:205)
at MapTest.main(MapTest.java:15)

Is this supposed to happen? Any help is appreciated.

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 2 2003
Added on Apr 4 2003
2 comments
84 views