I have a class Student
which does not implement Comparable
.
Case 1: I created a TreeSet
of Student
objects. mySet.add(s1)
does not throw error at compile time, but throws error at runtime. (I could find explanations for this eg: this)
Student s1= new Student(12,"ABCD");
Student s2= new Student(2,"EFGH");
Set<Student> mySet=new TreeSet<Student>();
mySet.add(s1);//shows error only at run time
Case 2:
I created an ArrayList
of Student
objects and tried to sort using Collections.sort(myList)
. Now, this shows an error at compile time itself.
Student s1= new Student(12,"ABCD");
Student s2= new Student(2,"EFGH");
List<Student> myList=new ArrayList<Student>();
myList.add(s1);
myList.add(s2);
Collections.sort(myList); //shows error in the IDE
Why is there a difference in the two cases?