[英]Java best practice when to use Lambda exprection
I have a case where service which returns optional and that value is used only for the condition only and if true return success else throws an exception, so for this do I need to use a lambda expression.我有一个案例,其中返回可选的服务,并且该值仅用于条件,如果 true 返回成功则抛出异常,因此为此我需要使用 lambda 表达式。
Optional<MyClass> val = service.getMyclass();
if(val.isPresent) {
//some operation
}else{
//throw exception
}
try below given code.尝试下面给出的代码。
Optional<MyClass> var = service.getMyclass();
MyClass val = var.orElseThrow(RuntimeException::new);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.