Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8140283

add Stream::transform method to facilitate fluent chaining of other methods



    • Enhancement
    • Resolution: Unresolved
    • P4
    • None
    • None
    • core-libs


      People like Optional and Stream because they allow chains of method calls. However, chaining only works for methods that are predefined. If an application wants to have a method or function that does something on a Stream (or Optional) and returns something that itself is chainable, the call to that method or function disrupts the nice linear chain.

      For example, consider a method that transforms a stream by conditionally adding a filter stage:

          <T,U> Stream<U> maybeAddFilter(Stream<T> s) {
              if (condition)
                  return s.filter(...);
                  return s;

      And suppose we have this pipeline:


      If we wanted to add the filter after the mapping operation, it would look like this:


      This disrupts the linear flow of the pipeline stages and makes the code confusing and hard to follow.

      This could be mitigated by having a function called (for example) "chain" that takes a function to be chained, calls it passing "this", and returning its return value. On Stream<T>, this function would be declared something like this:

          <U> chain(Function<Stream<T>,U> func);

      It would be used like so:


      The chain method on Stream<T> wouldn't necessarily return another Stream; it could return anything.


        Issue Links



              briangoetz Brian Goetz
              smarks Stuart Marks
              3 Vote for this issue
              4 Start watching this issue