我想做这样的事情:
private String getStringIfObjectIsPresent(Optional<Object> object){
object.ifPresent(() ->{
String result = "result";
//some logic with result and return it
return result;
}).orElseThrow(MyCustomException::new);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为ifPresent将Consumer功能接口作为参数,其具有void accept(T t).它不能返回任何值.还有其他办法吗?