Collecion as collector

Brian Goetz brian.goetz at oracle.com
Thu Apr 18 14:29:25 PDT 2013


We started here (with into(collection)) and discovered that approach had 
many flaws.

To name one, there's no way to make it parallel without guessing at the 
thread-safety of the target.

A Collector embodies information as to how to *create* a target 
collection.  In a parallel reduction, it may in fact create multiple 
smaller collections, and then merges them into one, which can be done 
safely even if the collections are not thread-safe (due to serial 
thread-confinement.)  This would be a sequential-only idiom, and we've 
worked very hard to make all the operations on streams work well either 
sequentially or parallel.

On 4/18/2013 5:07 PM, Jose wrote:
>
> I'm wonder why the Collection interface don't extend Collector using default
> methods. Al least a collection is the most obvious Collector you can
> imagine.
>
> This would allow adding elements to an existing collection using a
> straightforward idiom:
>
> Collection myCollection=..
>
> stram1.collect(myCollection);
> stram1.collect(myCollection);
>
> I have done this in custom classes that wrap a Collection and I feel it
> makes code more readable.
>
>
>
>


More information about the lambda-dev mailing list