Why is it so that java
don’t give compile time error when inserting objects in Tree
based collections.
It does gives runtime exception
. But wouldn’t it be great if it gives compile error.
Code :
import java.util.*;
class Dog{
int size;
Dog(int s){
size = s;
}
}
public class Test01 {
public static void main(String[] args) {
TreeSet set = new TreeSet();
set.add(new Dog(1));
set.add(new Dog(2));
set.add(new Dog(3));
}
}
It compiles perfect. On runtime it gives ClassCastException
complaining that Dog cannot be cast to java.lang.Comparable