在Optional.filter()之后调用方法而不传递对象 [英] Calling method after Optional.filter() without passing in object
本文介绍了在Optional.filter()之后调用方法而不传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以理解以下内容:
user
.filter(u -> "Sam".equals(u.getName()))
.ifPresent(this::doSomethingWithUser);
如果user.getName
等于"Sam"
,则将user
传递给本地方法doSomethingWithUser()
.
if user.getName
is equal to "Sam"
then pass user
to local method doSomethingWithUser()
.
但是,如果我只是想调用一个本地方法而不传递user
但要保留user.getName
等于"Sam"
的检查该怎么办?
However, what if I simply wanted to call a local method, without passing user
but keeping the check that user.getName
is equal to "Sam"
?
此用例是否使Optional
冗余?
推荐答案
只需忽略强制功能参数.这意味着您不能使用速记::
语法,并且会像这样:
Just ignore the mandatory function parameter. This means you cannot use shorthand ::
syntax and goes like this:
user
.filter(u -> "Sam".equals(u.getName()))
.ifPresent(u -> doSomethingWithoutUser());
这篇关于在Optional.filter()之后调用方法而不传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文