I have a list, and must find a specific object. I must sort list by students' averageMark
value, and create a function that
searches for student with second highest value. If it duplicates with another student, the younger one is being returned by function.
The other requirement of this task (to be solved properly) is that I can't create any object. Here is the code of class that I am working with:
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
public class Student implements Comparator<Student>{
private String firstName;
private String lastName;
private LocalDate dateOfBirth;
private double averageMark;
public double getAverageMark() {
return averageMark;
}
//function below must find student that I described upper. Currently
//function is incomplete because I still try to find correct solution
public static Student findSecondBestStudent(List<Student> students) {
return students.stream().sorted(Comparator.reverseOrder());
}
@Override
public int compare(Student o1, Student o2) {
return (int) (o1.getAverageMark() - o2.getAverageMark());
}
}
For now I did try to solve this by doing reverse sorting of the stream, then removing first value of stream and compare two next objects of stream and return the correct one.
I am able to solve it using a for loop, but this solution mismatches with condition of task(requires to create object)
Implement Comparable
rather than Comparator
.
Make your compareTo
function so it sorts the students in desired order (reversed)
Sort your stream
, skip 1 entry, return the next one.
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class Student implements Comparable<Student> {
private String firstName;
private String lastName;
private LocalDate dateOfBirth;
private double averageMark;
public Student(String firstName, String lastName, LocalDate dateOfBirth, double averageMark) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.averageMark = averageMark;
}
public double getAverageMark() {
return averageMark;
}
//function below must find student that I described upper. Currently
//function is incomplete because I still try to find correct solution
public static Student findSecondBestStudent(List<Student> students) {
return students.stream().sorted().skip(1).findFirst().orElse(null);
}
@Override
public int compareTo(Student other) {
if (other == null)
return 1;
if (other.averageMark == averageMark) {
if (other.dateOfBirth == null)
return -1;
return other.dateOfBirth.compareTo(dateOfBirth);
}
return Double.compare(other.averageMark, averageMark);
}
@Override
public String toString() {
return "Student [firstName=" + firstName + ", lastName=" + lastName + ", dateOfBirth=" + dateOfBirth
+ ", averageMark=" + averageMark + "]";
}
public static void main(String[] args) {
final List<Student> students = new ArrayList<>(6);
students.add(new Student("Peter", "Tibbitts", LocalDate.of(2001, 4, 6), 5));
students.add(new Student("Leon", "Gaston", LocalDate.of(1951, 6, 17), 12));
students.add(new Student("Eric", "Carter", LocalDate.of(1945, 12, 24), 9));
students.add(new Student("Richard", "Heard", LocalDate.of(1984, 5, 9), 4));
students.add(new Student("Frankie", "Bonner", LocalDate.of(1970, 4, 19), 10));
students.add(new Student("Donald", "Pascal", LocalDate.of(2000, 3, 26), 10));
final Student result = Student.findSecondBestStudent(students);
System.out.println(result);
}
}