Here's my code:
public static void main(String[] args){
TreeSet<Dog> d = new TreeSet<Dog>();
d.add(new Dog());
System.out.println(d.size());
}
class Dog{
}
As you can see, the Dog
class is not a Comparable
object and I sure did not specify a Comparator
to be used for sorting. How does this snippet of code run without exceptions? It printed out 1
.
I tried adding another Dog
to the TreeSet and it threw a ClassCastException
as expected.
EDIT: I'm using Java 6