Você tem um objeto Student que precisa ser armazenado num Set e ordenado por idade. Para tal voce decide usar um TreeSet com um Comparator. Complete o código abaixo:
public class Student {
public int id; // the unique ID of the student...
public int age; // the student's age...
public Student(int id, int age) {
this.id = id;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (o instanceof Student) {
Student s = (Student) o;
return s.id == this.id;
}
return false;
}
@Override
public int hashCode() {
return id;
}
@Override
public String toString() {
return "[Student id=" + id + ";age=" + age + "]";
}
private static class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
// ====================
// YOUR CODE GOES HERE
// ====================
}
}
public static void main(String[] args) {
Student s1 = new Student(1, 20);
Student s2 = new Student(2, 18);
Student s3 = new Student(3, 22);
Student s4 = new Student(4, 20);
Set<Student> set = new TreeSet<Student>(new StudentComparator());
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
for(Student s : set) {
System.out.println(s);
}
}
}



