mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8164934: Optional.map() javadoc code example
Reviewed-by: forax, chegar
This commit is contained in:
parent
d0b96b7e6a
commit
9555cf3b50
1 changed files with 9 additions and 9 deletions
|
@ -214,20 +214,20 @@ public final class Optional<T> {
|
|||
* @apiNote
|
||||
* This method supports post-processing on {@code 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
|
||||
* {@code Optional<FileInputStream>}:
|
||||
* following code traverses a stream of URIs, selects one that has not
|
||||
* yet been processed, and creates a path from that URI, returning
|
||||
* an {@code Optional<Path>}:
|
||||
*
|
||||
* <pre>{@code
|
||||
* Optional<FileInputStream> fis =
|
||||
* names.stream().filter(name -> !isProcessedYet(name))
|
||||
* Optional<Path> p =
|
||||
* uris.stream().filter(uri -> !isProcessedYet(uri))
|
||||
* .findFirst()
|
||||
* .map(name -> new FileInputStream(name));
|
||||
* .map(Paths::get);
|
||||
* }</pre>
|
||||
*
|
||||
* Here, {@code findFirst} returns an {@code Optional<String>}, and then
|
||||
* {@code map} returns an {@code Optional<FileInputStream>} for the desired
|
||||
* file if one exists.
|
||||
* Here, {@code findFirst} returns an {@code Optional<URI>}, and then
|
||||
* {@code map} returns an {@code Optional<Path>} for the desired
|
||||
* URI if one exists.
|
||||
*
|
||||
* @param mapper the mapping function to apply to a value, if present
|
||||
* @param <U> The type of the value returned from the mapping function
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue