RFR: 8272137: Make Collection and Optional classes streamable

liach github.com+7806504+liach at openjdk.java.net
Tue Aug 17 06:50:30 UTC 2021


On Mon, 9 Aug 2021 12:28:23 GMT, CC007 <github.com+5381337+CC007 at openjdk.org> wrote:

> create Streamable and ParallelStreamable interface and use them in Collection and Optional

With modern java, you can always create a `Streamable` on your own (and have specifications/documentations that a simple `Supplier` lacks) and implement it like:

public interface Streamable<T> {
  Stream<T> stream();
  static Streamable<T> ofIterable(Iterable<T> iterable) {
    return () -> StreamSupport.stream(iterable.spliterator(), false);
  }
}

The lack of such an interface in the JDK is not really a problem.

> Are you suggesting that there are deeper architectural issues with the hierarchy-heavy collection stack?

I am saying that your sample project has unnecessarily many interfaces for your collection model, where many of them have little virtue on their own. This interface mash is susceptible to accidentally introducing unwanted features into the hierarchy and having method or specification clashes for implementation.

Looking back on identifying efficiently streamable objects, people can usually find efficient streams by the method return types. Your interface doesn't allow people to identify `BufferedReader.lines()` as an efficiently streamable target, for instance.
In addition, I think the main purpose of `Optional.stream()` is to allow using it in stream flatmapping than to promote stream creation with optionals.

-------------

PR: https://git.openjdk.java.net/jdk/pull/5050


More information about the core-libs-dev mailing list