Java 为什么在 Optional.ofNullable 上使用 Optional.of?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31696485/
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
Why use Optional.of over Optional.ofNullable?
提问by whirlwin
When using the Java 8 Optional
class, there are two ways in which a value can be wrapped in an optional.
使用 Java 8Optional
类时,有两种方法可以将值包装在可选项中。
String foobar = <value or null>;
Optional.of(foobar); // May throw NullPointerException
Optional.ofNullable(foobar); // Safe from NullPointerException
I understand Optional.ofNullable
is the only safe way of using Optional
, but why does Optional.of
exist at all? Why not just use Optional.ofNullable
and be on the safe side at all times?
我明白Optional.ofNullable
是唯一安全的使用方式Optional
,但为什么Optional.of
存在呢?为什么不只是使用Optional.ofNullable
并始终保持安全?
采纳答案by Tagir Valeev
Your question is based on assumption that the code which may throw NullPointerException
is worse than the code which may not. This assumption is wrong. If you expect that your foobar
is never null due to the program logic, it's much better to use Optional.of(foobar)
as you will see a NullPointerException
which will indicate that your program has a bug. If you use Optional.ofNullable(foobar)
and the foobar
happens to be null
due to the bug, then your program will silently continue working incorrectly, which may be a bigger disaster. This way an error may occur much later and it would be much harder to understand at which point it went wrong.
你的问题是基于这样的假设,即可能抛出NullPointerException
的代码比可能不会抛出的代码更糟糕。这个假设是错误的。如果您期望foobar
由于程序逻辑而永远不会为空,那么使用它会更好,Optional.of(foobar)
因为您将看到NullPointerException
指示您的程序有错误的 a 。如果您使用Optional.ofNullable(foobar)
并且foobar
恰好是null
由于该错误引起的,那么您的程序将默默地继续错误地工作,这可能是一个更大的灾难。这样,错误可能会发生得更晚,并且更难理解在哪个点出错了。
回答by Karan Arora
In addition, If you know your code should not work if object is null, you can throw exception by using Optional.orElseThrow
此外,如果您知道如果 object 为 null,您的代码不应该工作,您可以通过使用抛出异常 Optional.orElseThrow
String nullName = null;
String name = Optional.ofNullable(nullName).orElseThrow(NullPointerException::new);
@tagir-valeev, Please correct if I am wrong.
@ tagir-valeev,如果我错了,请纠正。
回答by espendennis
Optional should mainly be used for results of Services anyway. In the service you know what you have at hand and return Optional.of(someValue) if you have a result and return Optional.empty() if you don't. In this case, some value should never be null and still, you return an Optional.
Optional 应该主要用于服务的结果无论如何。在服务中,您知道手头有什么,如果有结果则返回 Optional.of(someValue),如果没有则返回 Optional.empty()。在这种情况下,某些值永远不应该为空,并且仍然返回一个 Optional。
回答by Gautam Chaurasia
This depends upon scenarios.
这取决于场景。
Let's say you have some business functionality and you need to process something with that value further but having null
value at time of processing would impact it.
假设您有一些业务功能,您需要进一步处理具有该价值的东西,但null
在处理时具有价值会影响它。
Then, in that case, you can use Optional<?>
.
然后,在这种情况下,您可以使用Optional<?>
.
String nullName = null;
String name = Optional.ofNullable(nullName)
.map(<doSomething>)
.orElse("Default value in case of null");