java 了解 Optionals.orElse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37485166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding Optionals.orElse
提问by silentsudo
I am trying to learn Java 8 feature Optional. I am confused about how Optional.orElse is working. Here is what i have tried:
我正在尝试学习Java 8 功能 Optional。我对 Optional.orElse 的工作方式感到困惑。这是我尝试过的:
public class OptionalsExample {
public static void main(String[] args) {
Name userName = new Name();
userName.setName("John Doe");
Optional<Name> optionalName = Optional.ofNullable(userName);
optionalName.ifPresent(
(Name value) -> {
Optional<String> optionalNameString = Optional.ofNullable(value.getName());
optionalNameString.ifPresent(name -> System.err.println("Name is: " + optionalNameString.get()));
}
);
}
private static void printError() {
System.err.println("No Name present");
}
}
My Concern is that if name is set everything works fine but when no name is set i want to execute orElse. I want to do something when i comment
我担心的是,如果设置了 name 一切正常,但是当没有设置 name 时,我想执行orElse。我评论的时候想做点什么
userName.setName("John Doe");
like printing No Name Found
喜欢打印 No Name Found
How can i do that?
我怎样才能做到这一点?
TIA
TIA
回答by Holger
Your usage of nested ifPresent
calls is very similar to the (nested) forEach
calls on streams, seen in a lot of other questions.
您对嵌套ifPresent
调用的使用与对流的(嵌套)forEach
调用非常相似,可以在许多其他问题中看到。
They are a clear sign that you should try better on functional thinking.
它们清楚地表明你应该更好地尝试功能性思维。
What you want to do is
你想做的是
System.out.println(
Optional.ofNullable(userName) // will be an empty optional if userName is null
.map(Name::getName) // will turn to empty optional if getName returns null
.map("Name is: "::concat) // prepend "Name is: " (only when we have a name)
.orElse("No Name Found") // get the result string or the alternative
);
The important point to understand, is, that the map
ping steps applied to an empty Optional
don't do anything but return an empty Optional
again. Since you want to print in either case, the print statement is not passed as consumer, but written as stand-alone statement, printing the value returned by the orElse
invocation, which will be either, the non-null
result of the processing steps or the alternative String
.
要理解的重要一点是,map
应用于空的ping 步骤Optional
除了Optional
再次返回空之外什么都不做。由于您想在任何一种情况下都打印,因此打印语句不是作为消费者传递的,而是作为独立语句编写的,打印orElse
调用返回的值,这将是null
处理步骤的非结果或替代String
.
回答by explv
The orElse method allows you to return an alternative value when there isn't one in the Optional.
当 Optional 中没有替代值时,orElse 方法允许您返回一个替代值。
If you just want to print out an alternative String you can use the orElse method to return an alternative String:
如果您只想打印一个替代字符串,您可以使用 orElse 方法返回一个替代字符串:
Optional<String> optionalNameString = Optional.ofNullable(value.getName());
System.out.println(optionalNameString.orElse("Name is not present"));
Otherwise you can just check if the value is present using isPresent()
否则,您可以使用 isPresent() 检查该值是否存在
Optional<String> optionalNameString = Optional.ofNullable(value.getName());
if(optionalNameString.isPresent()) {
// do something
} else {
//do something else
}