collect signature

Brian Goetz brian.goetz at oracle.com
Fri Jun 14 13:21:30 PDT 2013


So, the implementation of collect() actually cares about what I is; 
therefore it makes sense for I to show up in the signature.  The 
intermediate type I (later renamed to A in Collector, but this hasn't 
propagated to Stream yet) in Collector is relevant to those implementing 
a Collector (like the implementations in Collectors), or implementing a 
reduction using Collector (like Stream.collect()), but most *users* of 
collect()-like facilities will not care about the specific intermediate 
accumulation type.  (In some cases, they just don't care (e.g., 
toList()), whereas in other cases, it actually should be hidden from 
them (e.g., that joining() uses StringBuilder and not StringBuffer). 
Methods that return a Collector can, but are not obligated to, indicate 
they are hiding this intermediate type by specifying ? for A in the type 
instantiation of Collector, as in: Collector<CharSequence, ?, String>, 
which means "Collector from CharSequence to String using an unknown 
intermediate type".  This is what we've done for all the methods in 
Collectors.)

There have been many workarounds proposed to "hide" (unfortunately only 
partially) the stray wildcard from the users' sensitive eyes, but so far 
all of these seem firmly in "cure is worse than the disease" territory.

On 6/14/2013 6:18 AM, Boaz Nahum wrote:
> Hi Brian.
>
> I guess you already have been asked this trillion times, but,
> why <I> appears in collect signature ?
>
> *<R, I> R collect(Collector<? super T, I, R> collector);*
>
> I tried to compile many examples(from Collectors) with:
>
> *<R> R collect(Collector<? super T, ?, R> collector);*
>
> and it just compiled fine.
>
> Thanks
> Boaz
>
>
>
> On Fri, Jun 14, 2013 at 2:20 AM, <brian.goetz at oracle.com> wrote:
>
>> Changeset: de85a85ab586
>> Author:    briangoetz
>> Date:      2013-06-13 19:20 -0400
>> URL:       http://hg.openjdk.java.net/lambda/lambda/jdk/rev/de85a85ab586
>>
>> Add post-transform functionality to Collector; eliminate STRICTLY_MUTATIVE
>> collector characteristic; restructure existing set of Collectors, including
>> various renamings; add StringJoiner.merge() method; more Collector tests
>>
>> ! src/share/classes/java/util/DoubleSummaryStatistics.java
>> ! src/share/classes/java/util/IntSummaryStatistics.java
>> ! src/share/classes/java/util/LongSummaryStatistics.java
>> ! src/share/classes/java/util/Optional.java
>> ! src/share/classes/java/util/StringJoiner.java
>> ! src/share/classes/java/util/stream/Collector.java
>> ! src/share/classes/java/util/stream/Collectors.java
>> ! src/share/classes/java/util/stream/DelegatingStream.java
>> ! src/share/classes/java/util/stream/ReduceOps.java
>> ! src/share/classes/java/util/stream/ReferencePipeline.java
>> ! src/share/classes/java/util/stream/Stream.java
>> ! test/java/lang/StringBuilder/AddAllTest.java
>> ! test/java/util/StringJoiner/BasicTest.java
>> ! test/java/util/stream/bootlib/java/util/stream/OpTestCase.java
>> !
>> test/java/util/stream/test/org/openjdk/tests/java/util/FillableStringTest.java
>> !
>> test/java/util/stream/test/org/openjdk/tests/java/util/stream/GroupByOpTest.java
>> !
>> test/java/util/stream/test/org/openjdk/tests/java/util/stream/SummaryStatisticsTest.java
>> !
>> test/java/util/stream/test/org/openjdk/tests/java/util/stream/TabulatorsTest.java
>> ! test/jdk/lambda/MethodReferenceTestInstanceMethod.java
>> ! test/jdk/lambda/separate/TestHarness.java
>>
>>
>>
>


More information about the lambda-dev mailing list