![2dd32c8f33b668726c35b80ee0cdeed1.png](https://i-blog.csdnimg.cn/blog_migrate/e4da4e803bf236a5cf815e4d07739572.jpeg)
过滤操作产生过滤流,即输入流的子集,其元素对于指定的谓词计算为true。
predicate是一个接受元素并返回布尔值的函数。
过滤的流具有与输入流相同的类型。
如果predicate对所有元素求值为false,它将生成一个空流。
以下代码仅在女性中使用员工流和过滤器。 它将女性映射到他们的名字,并将它们打印在标准输出上。
import java.time.LocalDate;import java.time.Month;import java.util.Arrays;import java.util.List;public class Main { public static void main(String[] args) { Employee.persons() .stream() .filter(Employee::isFemale) .map(Employee::getName) .forEach(System.out::println); }}class Employee { public static enum Gender { MALE, FEMALE } private long id; private String name; private Gender gender; private LocalDate dob; private double income; public Employee(long id, String name, Gender gender, LocalDate dob, double income) { this.id = id; this.name = name; this.gender = gender; this.dob = dob; this.income = income; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Gender getGender() { return gender; } public boolean isMale() { return this.gender == Gender.MALE; } public boolean isFemale() { return this.gender == Gender.FEMALE; } public void setGender(Gender gender) { this.gender = gender; } public LocalDate getDob() { return dob; } public void setDob(LocalDate dob) { this.dob = dob; } public double getIncome() { return income; } public void setIncome(double income) { this.income = income; } public static List persons() { Employee p1 = new Employee(1, "Jake