프로그래밍/Back end
[Back end] Java Find Min and Max from List
Reference M1
2020. 9. 3. 11:37
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("Pea", 20));
studentList.add(new Student("Ni", 35));
studentList.add(new Student("Pang", 30));
studentList.add(new Student("Tum", 25));
studentList.add(new Student("Sombut", 22));
Student student = Collections.max(studentList, Comparator.comparing(s -> s.getAge()));
System.out.println("The older student is : " + student.getName());
student = Collections.min(studentList, Comparator.comparing(s -> s.getAge()));
System.out.println("The youngest student is : " + student.getName());
}