I'm trying to remove oldest Student from my TreeSet but the object is not getting deleted. Can someone please explain how to remove the oldest Student object from my TreeSet ?
public class Student implements Comparable<Student> {
private String studentId;
private String studentAge;
public Student(String studentId, String studentAge) {
super();
this.studentId = studentId;
this.studentAge = studentAge;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentAge() {
return studentAge;
}
public void setStudentAge(String studentAge) {
this.studentAge = studentAge;
}
@Override
public int compareTo(Student o) {
if (Double.valueOf(studentAge).compareTo(Double.valueOf(o.getStudentAge())) == 0) {
return 1;
} else
return Double.valueOf(studentAge).compareTo(Double.valueOf(o.getStudentAge()));
}
@Override
public boolean equals(Object obj) {
Student student = (Student) obj;
return (student.getStudentId().equals(studentId) && student.getStudentAge().equals(studentAge));
}
@Override
public String toString() {
return "StudentId " + studentId + " Student Age " + studentAge;
}
}
import java.util.TreeSet;
public class MainApp {
public static void main(String[] args) {
TreeSet<Student> studentList = new TreeSet<Student>();
studentList.add(new Student("10001", "5"));
studentList.add(new Student("10001", "15"));
studentList.add(new Student("10001", "25"));
studentList.add(new Student("10001", "3"));
studentList.add(new Student("10001", "2"));
studentList.add(new Student("10001", "1"));
studentList.add(new Student("10001", "10"));
studentList.add(new Student("10001", "6"));
studentList.add(new Student("10001", "7"));
studentList.add(new Student("10001", "2"));
System.out.println(studentList.last().getStudentAge());
System.out.println(studentList);
Student maxAge = studentList.last();
System.out.println(studentList.remove(new Student(maxAge.getStudentId(), maxAge.getStudentAge())));
System.out.println(studentList);
}
}