代码演示:根据性别筛选出所有的女同学
@Data class Student{ public Integer age; public String name; public String sex; } Liststudents = new ArrayList<>(); Student student1 = new Student(); student1.setAge(18); student1.setName("赵"); student1.setSex("男"); Student student2 = new Student(); student2.setAge(19); student2.setName("钱"); student2.setSex("女"); Student student3 = new Student(); student3.setAge(20); student3.setName("孙"); student3.setSex("女"); Collections.addAll(students,student1,student2,student3); //根据条件过滤 出所有的女同学 List result = students.stream().filter(student -> student.getSex().equals("女")).collect(Collectors.toList()); System.out.println(result);
执行结果:
filter方法:对流的过滤,将一个流转换成另一个子集流
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)