- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个java应用程序,我发现了这两个选项:
一个是jdk原生的Optional.ofNullable,另一个是来自guava库的Optional.fromNullable。
简单来说,它们的意思相同吗?在非 java8 应用程序中使用 fromNullable 相当于 ofNullabe?
阅读我找到的文档:
https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (return a default value if value not present) and ifPresent() (execute a block of code if the value is present).
This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of Optional may have unpredictable results and should be avoided.
对比
https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/Optional.html
An immutable object that may contain a non-null reference to another object. Each instance of this type either contains a non-null reference, or contains nothing (in which case we say that the reference is "absent"); it is never said to "contain null".
A non-null Optional<T> reference can be used as a replacement for a nullable T reference. It allows you to represent "a T that must be present" and a "a T that might be absent" as two distinct types in your program, which can aid clarity.
Some uses of this class include
As a method return type, as an alternative to returning null to indicate that no value was available
To distinguish between "unknown" (for example, not present in a map) and "known to have no value" (present in the map, with value Optional.absent())
To wrap nullable references for storage in a collection that does not support null (though there are several other approaches to this that should be considered first)
A common alternative to using this class is to find or create a suitable null object for the type in question.
This class is not intended as a direct analogue of any existing "option" or "maybe" construct from other programming environments, though it may bear some similarities.
Comparison to java.util.Optional (JDK 8 and higher): A new Optional class was added for Java 8. The two classes are extremely similar, but incompatible (they cannot share a common supertype). All known differences are listed either here or with the relevant methods below.
在谈论空值处理时,您知道两者是否可以被视为“相同”吗?
最佳答案
Any idea if both can be taken as "same" when talking about nulls handling?
是的,它们与文档所述相同:
public static <T> Optional<T> fromNullable(@Nullable
T nullableReference)
If nullableReference is non-null, returns an Optional instance containing that reference; otherwise returns absent(). Comparison to java.util.Optional: this method is equivalent to Java 8's Optional.ofNullable.
关于java - Optional.ofNullable 与Optional.fromNullable 之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47860590/
这个问题在这里已经有了答案: How to convert Optional to Optional [closed] (3 个答案) 关闭 2 年前。 我正在尝试在 Optional.OfNull
我想知道使用Optional.ofNullable()是否有退出 for(String name: names){ if(name != null) person = "Mr./Mrs.
我可以使用 Java8 提取特定部分,如下所示 request.getBody() .getSections() .filter(section -> "name".equ
我想知道使用Optional.ofNullable()是否有退出 for(String name: names){ if(name != null) person = "Mr./Mrs.
有时我得到的值为 [null]对于 List employees 。为了防止我执行以下检查 List employees …. if((employees.size()>0 && employees.
我有一种方法来验证电子邮件的收件人。 在我的代码中 .map(Recipient::getId) 产生错误: Non static method cannot be reference from a
哪种空检查更可取? Optional.ofNullable(port).ifPresent(settings::setPort); 或 if (port != null) { settings.
我对 Optional.ofNullable 方法感到惊讶。有一天我写了一个应该返回 Optional 的函数: private Optional extractFirstValueFrom(Insi
我有一个嵌套类结构,用于反序列化我的数据: Class A has a single property that is an object of Class B (say b) Class B has
此 using optionals correctly is not optional 中的第 12 项文章状态 Sometimes, we tend to "over-use" things. Me
java.util Optional.ofNullable 是否与 Mockito 一起正常工作? 在代码执行过程中,我遇到了这样的事情: User user = Optional.ofNullabl
考虑这个表达式的用法: String hi = Optional.ofNullable(sayHi()).orElse("-"); 有效对应于这个三元表达式: String hi = sayHi()
没有的原因是什么: OptionalInt.ofNullable(Integer); 如果您想将可选/可为空的 Integer 转换为 OptionalInt,这似乎是一个完美的选择。我目前正在使用我
首先,我知道这两种方法的区别。 Optional.of : 用于保证不存在null,如果输入null,nullPointExcepction Optional.ofNullable :可能为空,也可能
我正在开发一个java应用程序,我发现了这两个选项: 一个是jdk原生的Optional.ofNullable,另一个是来自guava库的Optional.fromNullable。 简单来说,它们的
我对 Java 8 可选有疑问。 下面的代码给出编译错误: Integer number = Optional.ofNullable(new Integer(10)); 但是当我们执行以下操作时,它不
这个问题已经有答案了: Optional.of(null)will throw NPE, I need to verify null before call the method? (2 个回答) 已
这个问题在这里已经有了答案: Uses for Optional (14 个答案) 关闭 5 年前。 我最近看到一个blog post ( tweeted by @java ) 这表明以下代码正变得
我注意到如果我按以下方式使用 Optional: Object returnValue = Optional.ofNullable(nullableValue) .map(nonNu
我有一个关于 Java 8 的问题 Optional ,其目的是解决 NullPointerException 异常。 问题是,让我们选择两种类型的原因是什么: Optional.of(T value
我是一名优秀的程序员,十分优秀!