RFR 8050819: Please add java.util.Stream.ofNullable(T object)

Stuart Marks stuart.marks at oracle.com
Tue Jan 27 22:49:49 UTC 2015

On 1/26/15 2:15 PM, Remi Forax wrote:
> On 01/26/2015 07:42 PM, Alan Bateman wrote:
>> On 26/01/2015 18:32, Paul Sandoz wrote:
>>> On Jan 20, 2015, at 7:05 PM, Paul Sandoz <Paul.Sandoz at oracle.com> wrote:
>>>> Hi,
>>>> http://cr.openjdk.java.net/~psandoz/jdk9/JDK-8050819-Stream-ofNullable/webrev/
>>>> Stream.ofNullable can make it easier to work fluently when streams are
>>>> produced, for example within flatMap.
>>> So far there have been "null" reviews :-)

I think I saw an empty review fly by. :-)

One quick note on the tests: the test data factory is

     TestData.Factory.ofSupplier("[0, 1)", ...)

which would seem to me to contain a single value zero, but they all use one as 
the value. Not that it really matters, but I was puzzling over it.

>> This looks okay to me, and the naming consistent with Optional.
>> -Alan.
> I'm not sure this method pull it's own weight,
> it can be written like this:
>    Optional.ofNullable(t).stream()

It's certainly possible, but it seems like an unnecessary level of indirection. 
The need for something like this has come up multiple times, and it seems 
logical for it to be next to Stream.of() and Stream.empty().

> I'm pretty sure that the JIT will remove the creation of the intermediary Optional
> and adding a method in the JDK that considers that null is an acceptable value
> is in my opinion the wrong signal to send.

I guess having a method like this makes it easier for people to deal with their 
possibly-null-producing code in a new streams world. Such null-producing code 
already exists. I guess you could say that providing this method encourages 
people to continue producing nulls, but everybody is already doing this today, 


More information about the core-libs-dev mailing list