Yet another run at reduce (collect)

Brian Goetz brian.goetz at oracle.com
Wed Jan 9 15:10:23 PST 2013


We have tried to follow the practice of treating all objects passed to 
stream methods as values.  So collectors can be reused.  You can 
singleton them as you suggest.

On 1/9/2013 5:50 PM, Aleksey Shipilev wrote:
> Thanks. Another question: it seems to me that collector passed into
> collect(...) can actually be reused, since I would expect the library
> will call makeResult() at least once to bootstrap the collector? Or, is
> the library reusing the one passed via the parameter? I.e. instead of:
>
>   stream().collect(new MyCollector()).getResult();
>
>   class MyCollector implements Collector<Long, MyCollector> {
>      public MyCollector makeResult() {
>          return new MyCollector();
>      }
>
>      public void accumulate(MyCollector result, Long value) {
>          result.result += value;
>      }
>
>      public MyCollector combine(MyCollector result, SumLongSink other) {
>          result.result += other.result;
>          return result;
>      }
>
>      public long getResult() {
>          return result;
>      }
>   }
>
> ...can I do this?
>
>   static final MyCollector MY_COLLECTOR = new MyCollector()
>   (...)
>   stream().collect(MY_COLLECTOR).getResult();
>
>
> -Aleksey.
>
> On 01/10/2013 01:13 AM, Brian Goetz wrote:
>> The simple rule is:
>>   - makeResult should return a new result container for every call
>>   - combine may return one of its arguments, or a new result
>>
>>
>>
>> On 1/9/2013 4:10 PM, Aleksey Shipilev wrote:
>>> On 01/09/2013 11:25 PM, Doug Lea wrote:
>>>> On 01/09/13 13:56, Brian Goetz wrote:
>>>>> I've pushed a rename to collect().  Check it out.
>>>>
>>>> Yay! Thanks!
>>>
>>> Ok, this is quite an intrusive change to adapt for my performance tests.
>>> Can you please add more Javadocs for Collector? In fact, I need more
>>> details on:
>>>    a) Should makeResult() return the new result on every call?
>>>    b) Can combine(result, other) return updated $result, or it should
>>> return distinct collectable?
>>>
>>> -Aleksey.
>>>
>


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