Forum Stats

  • 3,855,563 Users
  • 2,264,523 Discussions
  • 7,906,067 Comments

Discussions

What Comparators are defaulted?

843793
843793 Member Posts: 41,732 Green Ribbon
edited Apr 4, 2003 10:18PM in Generics
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

This discussion has been closed.