Why didn't Stream have a toList() method?

java, java-8, java-stream, list

Solution

Recently I wrote a small library called StreamEx which extends Java streams and provides this exact method among many other features:

StreamEx.of(-2,1,2,-5)
    .filter(n -> n > 0)
    .map(n -> n * n)
    .toList();

Also toSet(), toCollection(Supplier), joining(), groupingBy() and other shortcut methods are available there.

Problem

When using the Java 8 streams, it's quite common to take a list, create a stream from it, do the business and convert it back. Something like: ``` Stream.of(-2,1,2,-5) .filter(n -> n > 0) .map(n -> n * n) .collect(Collectors.toList()); ``` Why there is no short-cut/convenient method for the '`.collect(Collectors.toList())`' part? On Stream interface, there is method for converting the results to array called `toArray()`, why the `toList()` is missing? IMHO, converting the result to list is more common than to array. I can live with that, but it is quite annoying to call this ugliness. Any ideas?

Original source

Related problems