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

Double nested Stream.flatMap buffer the entire Stream before processing it

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Microsoft Windows 10 Pro 10.0.19042

      A DESCRIPTION OF THE PROBLEM :
      Nesting a `flatMap` call inside another `flatMap` produces a complete stream buffering as the one described at https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8075939

      The inner Stream seems to be completely evaluated, making it impossible to use Streams comfortably in some lazy-evaluated applications (cartesian product, for instance) that would seem natural given the API.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute a `flatMap` operation nested in another `flatMap` operation.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      a
      ax
      axx
      axxx
      axxxx
      ACTUAL -
      OutOfMemoryException as sd::repeat is evaluated from start to finish filling the heap with repetitions of the string "x".

      ---------- BEGIN SOURCE ----------
          public static void main(String[] args) {
              var max = Integer.MAX_VALUE;
              Stream.of("a", "b", "c")
                  .flatMap(s -> Stream.of("x", "y")
                      .flatMap(sd -> IntStream.rangeClosed(0, max)
                          .mapToObj(sd::repeat)))
                  .map(s -> s + "u")
                  .limit(5)
                  .forEach(System.out::println);
          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None found using the Streams API.

      FREQUENCY : always


            psandoz Paul Sandoz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: