过滤数据
小于 1 分钟函数式编程Optional
filter方法
public static void main(String[] args) throws Throwable {
Optional<Author> author = getAuthor();
System.out.println(author);
Optional<Author> optional = author.filter(au -> au.getAge() > 40);
System.out.println(optional);
}
private static Optional<Author> getAuthor() {
//数据初始化
Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
return Optional.ofNullable(author);
}
结果:
Optional[Author(id=1, name=蒙多, age=33, intro=一个从菜刀中明悟哲理的祖安人, books=null)]
Optional.empty
Process finished with exit code 0