max和min函数
最⼤值和最⼩值
小例子
public class Main {
    public static void main(String[] args) throws Exception {
        List<Student> list = Arrays.asList(new Student(32), new Student(33), new Student(21), new Student(29), new Student(18));
        //list.stream().max(Comparator.comparingInt(Student::getAge));
        Optional<Student> optionalStudent1 =  list.stream().max((s1, s2)->{
            return Integer.compare(s1.getAge(),s2.getAge());
        });
        Optional<Student> optionalStudent2 = list.stream().min((s1, s2) -> {
            return Integer.compare(s1.getAge(), s2.getAge());
        });
        Student student1 = optionalStudent1.get();
        Student student2 = optionalStudent2.get();
        System.out.println(student1.getAge());
        System.out.println(student2.getAge());
    }
}
class Student {
    private int age;
    public Student() {
    }
    public Student(int age) {
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
} 输出结果

本文作者为DBC,转载请注明。
