Hi this is a question that I found from a book. I am not exactly clear with thing happens in compareTo() method. Tree set is a sorted class. So does it always call to compareTo()?
And inside compareTo() how does sorting is done based on, return age-((Person)0).age ?
package javaapplication1;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;
public class _157 {
public static void main(String args[]){
NavigableSet s=new TreeSet();
for(int i=50;i<55;++i){
s.add(new Person(i));
}
SortedSet s2=s.tailSet(new Person(52));
System.out.println(s2);
}
}
class Person implements Comparable{
private int age;
@Override
public int compareTo(Object t) {
return ((Person)t).age-age;
}
Person(int age){
this.age=age;
}
public String toString(){
return "Person("+age+")";
}
}