Collecion as collector

Jose jgetino at
Fri Apr 19 02:55:29 PDT 2013

Brian, thanks for your comments and your time.

It's clear that the collector generated by the framework is safe por
parallel proccesing and it should be the first choice when there is no a
collection around. 
But on the other hand I feel that overprotecting the programmer from himself
can cause tensions in the framework that makes the API less fluent.

For this case in particular:

- I think it should be the programmer's resposability to use a concurrent
collection if he wants to collect a parallel stream. 
  I already have to do it in many places of my code that has nothing to do
with streams. At the very moment I get a 
  ConcurrentModificationException I know where the problem is.

- All the limitations imposed to the API to prevent a buggy recollection of
the elments of the stream would fail in their objetive if the user writes
  very simple code:


I want to note that I have nothing against the Collector interface, at
contrary, once undertod the concept I find it is a fundamental abstraction,
at the same level of Iterable. 
Now, in my mental representation of a Collection, I see an Iterable to get
elements form it and a Collector to put them inside.


-----Mensaje original-----
De: Brian Goetz [mailto:brian.goetz at] 
Enviado el: jueves, 18 de abril de 2013 23:29
Para: Jose
CC: lambda-dev at
Asunto: Re: Collecion as collector

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