RFR(m): 8140281 deprecate Optional.get()

Peter Levart peter.levart at gmail.com
Fri Apr 29 11:11:24 UTC 2016

On 04/29/2016 11:23 AM, Andrej Golovnin wrote:
> The lines 406-415 can be rewritten as:
>        otherMods
>            .stream()
>            .map(finder::find) // After this step you can filter out all
>                               // empty Optionals.
>                               // But I don't think it is really needed.
>            .forEach(
>                o -> o.ifPresent(mref -> {
>                    map.putIfAbsent(mref.descriptor().name(), ref);
>                    mrefs.add(mref);
>                }
>            );

...even more Stream-y (with JDK 9 changes to Optional):

     .flatMap(mod -> finder.find(mod).stream())
     .forEach(mref -> ...);

Yes, it takes some time for people to get used to new tools before they 
use them optimally. This is a normal learning process. So I wonder 
whether it is only the method name that is to blame for the observed 
percentage of wrong or sub-optimal usages of Optional or would it be 
more-or-less the same with a more descriptive name at this time.

But the argument that getWhenPresent() is easier to spot than get() when 
reading code is on spot so at least some bugs would get spotted more 
quickly if the method stood out.

Regards, Peter

More information about the core-libs-dev mailing list