RFR : JDK-8001642 : Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong

Howard Lovatt howard.lovatt at gmail.com
Wed Mar 6 16:02:35 PST 2013


Instead of adding average, first, etc. as methods to Stream you could
instead provide:

1. average etc. as Collectors that return a *new* stream that either
contains the average, etc. or is empty.
2. first etc. as Collectors that return a value or call Supplier if there
is no value to return (empty stream)

Usage:

final R first = stream.collect(AVERAGE).collect(first(() -> new R());

This reduces the number of methods, eliminates the need for the Optional
classes, and is very flexible because the Supplier can throw or provide a
value. Also you can produce an Optional, or anything else, if desired via a
second collect:

final Optional<R> optional = stream.collect(AVERAGE).collect(OPTIONAL);

 -- Howard.


On 7 March 2013 06:44, Remi Forax <forax at univ-mlv.fr> wrote:

> On 03/06/2013 08:34 PM, Brian Goetz wrote:
>
>>
>>> For Option lovers, one way to view this: it enables someone to provide
>>> their own Option instead of the one we provide. Right? If not, then I'm
>>> less favorable.
>>>
>>>  No, not right.  It prevents people from distinguishing between a stream
>> that is empty and a stream containing only the "orElse" value.  Just like
>> Map.get() prevents distinguishing between "not there" and "mapped to null."
>>
>>
> I don't know if 'it' is my proposal or not.
> If it is, yes you can use any Option implementations you want because you
> know if the value is present or not and if the value is present, you know
> the value. So you can return the Option implementation you want.
>
> Rémi
>
>


-- 
  -- Howard.


More information about the lambda-libs-spec-observers mailing list