- What is optional in java
- Method Summary
- Methods declared in class java.lang.Object
- Method Detail
- empty
- of
- ofNullable
- get
- isPresent
- isEmpty
- ifPresent
- ifPresentOrElse
- filter
- map
- flatMap
- or
- stream
- orElse
- orElseGet
- orElseThrow
- orElseThrow
- equals
- hashCode
- toString
- Method Summary
- Methods declared in class java.lang.Object
- Method Details
- empty
- of
- ofNullable
- get
- isPresent
- isEmpty
- ifPresent
- ifPresentOrElse
- filter
- map
- flatMap
- or
- stream
- orElse
- orElseGet
- orElseThrow
- orElseThrow
- equals
- hashCode
- toString
What is optional in java
A container object which may or may not contain a non- null value. If a value is present, isPresent() returns true . If no value is present, the object is considered empty and isPresent() returns false . Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a 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.
Method Summary
If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional .
If a value is present, returns the result of applying the given Optional -bearing mapping function to the value, otherwise returns an empty Optional .
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
If a value is present, returns an Optional describing (as if by ofNullable(T) ) the result of applying the given mapping function to the value, otherwise returns an empty Optional .
If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
If a value is present, returns the value, otherwise returns the result produced by the supplying function.
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream .
Methods declared in class java.lang.Object
Method Detail
empty
of
ofNullable
get
isPresent
isEmpty
ifPresent
ifPresentOrElse
public void ifPresentOrElse(ConsumerT> action, Runnable emptyAction)
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
filter
public OptionalT> filter(PredicateT> predicate)
If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional .
map
public Optional map(FunctionT,? extends U> mapper)
If a value is present, returns an Optional describing (as if by ofNullable(T) ) the result of applying the given mapping function to the value, otherwise returns an empty Optional . If the mapping function returns a null result then this method returns an empty Optional .
API Note: This method supports post-processing on Optional values, without the need to explicitly check for a return status. For example, the following code traverses a stream of URIs, selects one that has not yet been processed, and creates a path from that URI, returning an Optional
Optional p = uris.stream().filter(uri -> !isProcessedYet(uri)) .findFirst() .map(Paths::get);
flatMap
public Optional flatMap(FunctionT,? extends Optional> mapper)
If a value is present, returns the result of applying the given Optional -bearing mapping function to the value, otherwise returns an empty Optional . This method is similar to map(Function) , but the mapping function is one whose result is already an Optional , and if invoked, flatMap does not wrap it within an additional Optional .
or
public OptionalT> or(Supplier extends OptionalT>> supplier)
If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
stream
If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream .
API Note: This method can be used to transform a Stream of optional elements to a Stream of present value elements:
Stream> os = .. Stream s = os.flatMap(Optional::stream)
orElse
orElseGet
public T orElseGet(Supplier extends T> supplier)
If a value is present, returns the value, otherwise returns the result produced by the supplying function.
orElseThrow
orElseThrow
public Throwable> T orElseThrow(Supplier exceptionSupplier) throws X extends Throwable
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
equals
- it is also an Optional and;
- both instances have no value present or;
- the present values are «equal to» each other via equals() .
hashCode
toString
Returns a non-empty string representation of this Optional suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Class Optional
A container object which may or may not contain a non- null value. If a value is present, isPresent() returns true . If no value is present, the object is considered empty and isPresent() returns false .
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present).
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
Method Summary
If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional .
If a value is present, returns the result of applying the given Optional -bearing mapping function to the value, otherwise returns an empty Optional .
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
If a value is present, returns an Optional describing (as if by ofNullable(T) ) the result of applying the given mapping function to the value, otherwise returns an empty Optional .
If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
If a value is present, returns the value, otherwise returns the result produced by the supplying function.
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream .
Methods declared in class java.lang.Object
Method Details
empty
of
ofNullable
get
isPresent
isEmpty
ifPresent
ifPresentOrElse
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
filter
If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional .
map
If a value is present, returns an Optional describing (as if by ofNullable(T) ) the result of applying the given mapping function to the value, otherwise returns an empty Optional . If the mapping function returns a null result then this method returns an empty Optional .
API Note: This method supports post-processing on Optional values, without the need to explicitly check for a return status. For example, the following code traverses a stream of URIs, selects one that has not yet been processed, and creates a path from that URI, returning an Optional
Optional p = uris.stream().filter(uri -> !isProcessedYet(uri)) .findFirst() .map(Paths::get);
flatMap
If a value is present, returns the result of applying the given Optional -bearing mapping function to the value, otherwise returns an empty Optional . This method is similar to map(Function) , but the mapping function is one whose result is already an Optional , and if invoked, flatMap does not wrap it within an additional Optional .
or
If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
stream
If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream .
API Note: This method can be used to transform a Stream of optional elements to a Stream of present value elements:
Stream> os = .. Stream s = os.flatMap(Optional::stream)
orElse
orElseGet
If a value is present, returns the value, otherwise returns the result produced by the supplying function.
orElseThrow
orElseThrow
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
equals
- it is also an Optional and;
- both instances have no value present or;
- the present values are «equal to» each other via equals() .
hashCode
toString
Returns a non-empty string representation of this Optional suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.