Java optional flatmap map

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 .

Читайте также:  Deploying java web application

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.

Источник

Java optional flatmap map

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.

Method Summary

If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional .

If a value is present, apply the provided Optional -bearing mapping function to it, return that result, otherwise return an empty Optional .

If a value is present in this Optional , returns the value, otherwise throws NoSuchElementException .

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result.

Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional .

Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.

Methods inherited from class java.lang.Object

Method Detail

empty

of

ofNullable

Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional .

get

If a value is present in this Optional , returns the value, otherwise throws NoSuchElementException .

isPresent

ifPresent

filter

public OptionalT> filter(PredicateT> predicate)

If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional .

map

public Optional map(FunctionT,? extends U> mapper)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return 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 file names, selects one that has not yet been processed, and then opens that file, returning an Optional :

 Optional fis = names.stream().filter(name -> !isProcessedYet(name)) .findFirst() .map(name -> new FileInputStream(name)); 

flatMap

public Optional flatMap(FunctionT,Optional> mapper)

If a value is present, apply the provided Optional -bearing mapping function to it, return that result, otherwise return an empty Optional . This method is similar to map(Function) , but the provided mapper is one whose result is already an Optional , and if invoked, flatMap does not wrap it with an additional Optional .

orElse

orElseGet

orElseThrow

public Throwable> T orElseThrow(Supplier exceptionSupplier) throws X extends Throwable

Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.

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.

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Оцените статью