public class Student { private int id; private String name; private String sex; private int age;
}
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //java8 查询年龄小于20的学生,并且根据年龄进行排序,得到学生姓名,生成新集合 List<Student> students = new ArrayList<>(); students.add(new Student(1,"张三","男",12)); students.add(new Student(2,"李四","女",35)); students.add(new Student(3,"王五","男",21)); students.add(new Student(4,"赵六","男",19)); List<String> result = students.stream() .filter(s->s.getAge() < 20)//过滤出年龄小于20的学生 .sorted(Comparator.comparing(Student::getAge))//根据年龄进行排序 .map(Student::getName)//得到学生姓名 .collect(Collectors.toList());//生成新集合 System.out.println(result); }