Source: own study
I am not getting what the
return s.compareTo(s); statement does. Is it comparing the same object against itself?
But the output of this program seems to be correct, which is:
[1, 22, 3, 4, 5] I guess the compareTo defined in the code is not used at all. Because, the TreeSet contains
String objects, and String class already implements Comparable. Is it so?
So, can I say that for TreeSet containing some third-party objects, the third-party class has to properly define the Comparable for sorting the elements? Else, the items are not sorted. Also, ClassCastException is thrown. Please correct me if I am wrong.