JDK-8164934 : Optional.map() javadoc code example is incorrect
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 8,9
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2016-08-18
  • Updated: 2017-06-06
  • Resolved: 2016-11-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b145Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :


A DESCRIPTION OF THE PROBLEM :
Is the example in API Note for map method on Optional class correct?
https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#map-java.util.function.Function-

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

new FileInputStream(name) in the map function throws java.io.FileNotFoundException (checked exception)


REPRODUCIBILITY :
This bug can be reproduced always.


Comments
An alternative example: * @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 URIs referencing files, selects one that has not * yet been processed, and then maps that URI to a file represented as a * {@code Path}, returning an {@code Optional<Path>}: * * <pre>{@code * Optional<Path> fis = * uris.stream().filter(name -> !isProcessedYet(name)) * .findFirst() * .map(Path::get)); * }</pre> * * Here, {@code findFirst} returns an {@code Optional<URI>}, and then * {@code map} returns an {@code Optional<Path>} for the desired * file if one exists.
29-08-2016

Checked this issue for 8,8u102,9ea on Windows 7 and could reproduce the issue as reported by the bug submitter. Steps to reproduce: ************************* - Compile the attached test application(JI_9042977.java) with JDK. Result: ********* OS : Windows 7 64 bit JDK: 8 b132 : Fail 8u102 b14 : Fail 9ea+126 : Fail -------------------------------------------------------------------------------------------------------------------------------------------------------- O/P: ****** C:\>java -version java version "1.8.0_102" Java(TM) SE Runtime Environment (build 1.8.0_102-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode) C:\>javac -Xlint:unchecked JI_9042977.java JI_9042977.java:11: warning: [unchecked] unchecked conversion ArrayList<String> names = new ArrayList(); ^ required: ArrayList<String> found: ArrayList JI_9042977.java:15: error: unreported exception FileNotFoundException; must be caught or declared to be thrown .map(name -> new FileInputStream(name)); ^ 1 error 1 warning
29-08-2016