什么是Optional.ofNullable和Optional.fromNullable之间的区别

时间:2017-12-17 23:52:23

标签: java

我正在开发一个Java应用程序,我发现了这两个选项:

一个来自jdk Optional.ofNullable和来自guava库的其他Optional.fromNullable。

简单来说,他们的意思是一样的吗?使用fromNullable和非java8应用程序是否等同于Nullabe?

阅读我发现的文档:

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.

VS

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.

在谈论空值处理时,是否可以将两者视为“相同”?

2 个答案:

答案 0 :(得分:1)

  

任何想法是否可以被视为&#34;相同&#34;在谈论空值时   处理?

是的,它们与文档相同:

fromNullable

public static <T> Optional<T> fromNullable(@Nullable
                           T nullableReference)
  

如果nullableReference为非null,则返回Optional实例   包含该参考;否则返回缺席()。 比较   java.util.Optional:此方法等同于Java 8   Optional.ofNullable

答案 1 :(得分:0)

取自番石榴文件:

与java.util.Optional(JDK 8及更高版本)的比较:

为Java 8添加了一个新的Optional类。这两个类非常相似,但不兼容(它们不能共享一个常见的超类型)。所有已知的差异都列在这里或下面的相关方法。

  • 此类是可序列化的; java.util.Optional不是。
  • java.util.Optional有其他方法ifPresent,filter,flatMap和orElseThrow。
  • java.util提供了原始专用版本OptionalInt,OptionalLong和OptionalDouble,建议使用它们;番石榴没有这些。

在可预见的将来,没有计划弃用此课程。但是,我们会尽可能地建议您更喜欢新的标准Java类。

请参阅“Guava用户指南”中有关使用Optional的文章。

换句话说,这些方法是等价的,类和实例不是。