gpt4 book ai didi

java - Optional.ofNullable 与Optional.fromNullable 之间有什么区别

转载 作者:行者123 更新时间:2023-12-02 11:51:23 26 4
gpt4 key购买 nike

我正在开发一个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?

是的,它们与文档所述相同:

fromNullable

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com