1、断点代码
public static void main(String[] args) {
List<Optional<Customer>> customers = Arrays.asList(
Optional.of(new Customer("萧炎", 18)),
Optional.of(new Customer("林动", 22)),
Optional.empty(),
Optional.of(new Customer("牧尘", 21)),
Optional.empty(),
Optional.of(new Customer("石昊", 23)),
Optional.empty()
);
long numberOf65PlusCustomers = customers
.stream()
.flatMap(c -> c
.map(Stream::of)
.orElseGet(Stream::empty))
.filter(c -> c.getAge() > 18)
.count();
System.out.println(numberOf65PlusCustomers);
}
public class Customer {
private String name;
private Integer age;
public Customer(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}