RFR: 8180352: Add Stream.toList() method

Tagir Valeev amaembo at gmail.com
Wed Nov 4 08:58:36 UTC 2020


Hello!

On Wed, Nov 4, 2020 at 2:55 AM Brian Goetz <brian.goetz at oracle.com> wrote:
> (In fact, we would be entirely justified to change the behavior of
> Collectors::toList tomorrow, to match the proposed Stream::toList; the
> spec was crafted to preserve this option.  We probably won't, because
> there are too many programmers who refuse to read the specification, and
> have baked in the assumption that it returns an ArrayList, and this
> would likely just be picking a fight for little good reason, regardless
> of who is right.)

My experience based on writing Stream code as well as reading Stream
code written by other developers suggest that one of the most popular
reasons to modify the resulting list is caused by the need to add a
special value (or a few of them) to the resulting list. We have
several options:
1. List<String> result =
Stream.concat(xyz.stream().filter(...).map(...),
Stream.of("special")).collect(toList());
Canonical, recommended way, no mutability. However concat is really
ugly, it breaks the fluent style of stream API calls, it's hard to
read and people just hate it.

2. List<String> result =
xyz.stream().filter(...).map(...).collect(toCollection(ArrayList::new));
result.add("special");
Mutable result, but much more readable. Also, according to spec. But
this ArrayList::new is hard to type and makes the code longer.

3. List<String> result = xyz.stream().filter(...).map(...).collect(toList());
result.add("special");
Shorter and readable! Well, violates the spec but works!

What people really need is an easy and readable way to concatenate
lists and streams, appending new elements, etc. The JDK doesn't offer
really great options here, so people often end up with external
utility methods or libraries.

Something like this would be very welcomed (and would reduce the need
for mutable lists):
List<String> result =
xyz.stream().filter(...).map(...).append("special").collect(toList());

As for nullity topic, I really welcome that the proposed toList() is
null-tolerant but it worth mentioning that existing terminal
operations like findFirst(), findAny(), min() and max() are already
null-hostile.

With best regards,
Tagir Valeev.


More information about the core-libs-dev mailing list