我似乎无法找到一种更简洁的方法来做到这一点
Optional<String> foo;
if (!foo.isPresent() || StringUtils.isBlank(foo.get())) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
有这个,但它实际上使逻辑更复杂,IMO:
Optional<String> foo;
if (!foo.filter(StringUtils::isNotBlank).isPresent()) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
觉得我错过了一些非常明显的东西.
也许这就是你要找的东西.
Optional.of("")
.filter(not(String::isBlank)))
.orElseThrow(() -> new MyException("..."));
Run Code Online (Sandbox Code Playgroud)
你会发现not下面Predicate.
显然,这比if发表声明效率低,但它有其自己的位置.
啊,好的,你的评论后更清楚了.使用orElseThrow.