私は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.
の両方が "同じ" ヌルの話をするときのように撮影することができた場合に任意のアイデア取り扱い?
うわー、あなたは質問をしています。次にjavadocに貼り付けて答えに導きます。おそらくあなたは*** javadocを読んでください***あなたは質問に貼り付けています!!!!!!ペーストされたjavadocの最後の行が開始されます。* "java.util.Optional'との比較" * ** javadocは、あなたが求めている比較を既に行っています。** * * –
Andreas
私はそれを読んだ、私のために明確ではなかった:) – jpganz18