2017-12-17 4 views
-1
対Optional.ofNullableの違い

私はJavaアプリケーションに取り組んでいます、と私は、この二つのオプションが見つかりました:いただきましOptional.fromNullable

1は、グアバライブラリからJDK Optional.ofNullableおよびその他のOptional.fromNullableからのネイティブです。簡単な用語で

、彼らは同じことを意味するのですか?非java8のアプリでfromNullableを使用すると、ofNullabeのequivalenteのですか?私が見つけたドキュメントを読ん

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. 

の両方が "同じ" ヌルの話をするときのように撮影することができた場合に任意のアイデア取り扱い?

+0

うわー、あなたは質問をしています。次にjavadocに貼り付けて答えに導きます。おそらくあなたは*** javadocを読んでください***あなたは質問に貼り付けています!!!!!!ペーストされたjavadocの最後の行が開始されます。* "java.util.Optional'との比較" * ** javadocは、あなたが求めている比較を既に行っています。** * * – Andreas

+0

私はそれを読んだ、私のために明確ではなかった:) – jpganz18

答えて

0

ヌルについて話すとき、どちらも「同じ」と考えることができますか? 取り扱いはどうですか?

はい、彼らは、ドキュメントの状態と同等だ:

fromNullable

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

nullableReferenceが非nullの場合、その参照を含む任意のインスタンス を返します。それ以外の場合はabsent()を返します。 java.util.Optionalに比較:このメソッドは、Java 8の Optional.ofNullableに相当します。 java.util.Optional(JDK 8以上)に

比較:

新しいオプションクラスは、Java 8の二つのクラスのために添加したグアバのドキュメントから取得

0

(彼らは、共通のスーパータイプを共有することはできません)非常に似ていますが、互換性がありません。すべての既知の相違点は、ここまたは以下の関連する方法のいずれかに記載されています。

  • このクラスは直列化可能です。 java.util.Optionalはそうではありません。
  • java.util.Optionalは追加の方法ifPresent、フィルタ、flatMap、およびorElseThrowを有しています。
  • java.utilには、プリミティブに特化したバージョンOptionalInt、OptionalLong、およびOptionalDoubleがあり、その使用が推奨されています。グアバはこれらを持っていません。

今後このクラスを廃止する予定はありません。ただし、できる限り新しい標準のJavaクラスを使用することをお勧めします。

「Guavaユーザーガイド」の「オプションの使用」を参照してください。

つまり、これらのメソッドは同等ですが、クラスとインスタンスは同じではありません。

関連する問題