基于条件和顺序的 Java 8 lambda 过滤
2022-09-02 09:49:51
我试图根据多个条件过滤列表,排序。
class Student{
private int Age;
private String className;
private String Name;
public Student(int age, String className, String name) {
Age = age;
this.className = className;
Name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
现在,如果我有一个清单,比如说
List<Student> students = new ArrayList<>();
students.add(new Student(24, "A", "Smith"));
students.add(new Student(24, "A", "John"));
students.add(new Student(30, "A", "John"));
students.add(new Student(20, "B", "John"));
students.add(new Student(24, "B", "Prince"));
我如何才能获得具有独特名称的最年长学生的列表?在C#中,使用System.Linq Group会非常简单,然后比较并与select一起扁平化,我不太确定如何在Java中实现相同的目标。