8050820: Please add java.util.Optional.stream() to convert Optional<T> to Stream<T>
Andreas Lundblad
andreas.lundblad at oracle.com
Wed Jan 21 10:00:00 UTC 2015
On Tue, Jan 20, 2015 at 06:59:52PM +0000, Paul Sandoz wrote:
> Hi,
>
> http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8050820-Optional-stream/webrev/
>
> This is one of those cases where i think adding a new method to Optional carries enough weight.
>
> It can be really awkward to use Optional with Stream.flatMap to map to a stream of present values.
>
> It's not at all obvious that one can do "op.map(Stream::of).orElse(Stream.empty()))" so often one sees some contorted code of there in the wild. Hopefully adding Optional.stream (and to the primitive variants) will help reduce such contortions.
>
> A CCC will be filed.
>
> Thanks,
> Paul.
Hi Paul,
My name is Andreas Lundblad, and I joined the langtools team a little more than a year ago. I'm not a Reviewer or anything but I casually read through your patch out of curiosity.
You don't seem to be a fan of the ?: operator. I would have written
return isPresent() ? Stream.of(value) : Stream.empty();
Any reason for if/else in this case? Also, I find it strange to include { ... } only on the else-branch:
+ if (!isPresent())
+ return Stream.empty();
+ else {
+ return Stream.of(value);
+ }
It's in all of the if-statements in the patch so perhaps it's intentional. I think it looks a bit peculiar.
-- Andreas
More information about the core-libs-dev
mailing list