Proposal to enhance Stream.collect

Rob Spoor openjdk at icemanx.nl
Sun Feb 24 11:29:56 UTC 2019


If you already know the size and are not going to parallelize your 
stream, you can simply use Collectors.toCollection:

Stream.of(1,2,3)
       .map(i -> i+1)
       .collect(Collectors.toCollection(() -> new ArrayList<>(3)));

It could perhaps be a bit easier if Collectors.toList would be 
overloaded to accept the initial size, but that would restrict the 
method to always use ArrayList (or another List with a predefined capacity).


On 23/02/2019 23:27, August Nagro wrote:
> Calling Stream.collect(Collector) is a popular terminal stream operation.
> But because the collect methods provide no detail of the stream's
> characteristics, collectors are not as efficient as they could be.
> 
> For example, consider a non-parallel, sized stream that is to be collected
> as a List. This is a very common case for streams with a Collection source.
> Because of the stream characteristics, the Collector.supplier() could
> initialize a list with initial size (since the merging function will never
> be called), but the current implementation prevents this.
> 
> I should note that the characteristics important to collectors are those
> defined by Spliterator, like: Spliterator::characteristics,
> Spliterator::estimateSize, and Spliterator::getExactSizeIfKnown.
> 
> One way this enhancement could be implemented is by adding a method
> Stream.collect(Function<ReadOnlySpliterator, Collector> collectorBuilder).
> ReadOnlySpliterator would implement the spliterator methods mentioned
> above, and Spliterator would be made to implement this interface.
> 
> For example, here is a gist with what Collectors.toList could look like:
> https://gist.github.com/AugustNagro/e66a0ddf7d47b4f11fec8760281bb538
> 
> ReadOnlySpliterator may need to be replaced with some stream specific
> abstraction, however, since Stream.spliterator() does not return with the
> correct characteristics. The below code returns false, for example (is this
> a bug?):
> 
> Stream.of(1,2,3).parallel().map(i ->
> i+1).spliterator().hasCharacteristics(Spliterator.CONCURRENT)
> 
> Looking forward to your thoughts,
> 
> - August Nagro


More information about the core-libs-dev mailing list