【发布时间】:2018-03-31 12:10:35
【问题描述】:
以下代码打印:
should Not have called
hello world
为什么orElse 会被执行,即使我们有一个包含值的 Optional?
public static void main(String[] args){
String o = Optional.ofNullable("world").map(str -> "hello" + str).orElse(dontCallMe());
System.out.println(o);
}
private static String dontCallMe() {
System.out.println("should Not have called");
return "why god why";
}
【问题讨论】: