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

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

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: 11, 16, 17
    • Fix Version/s: None
    • Component/s: core-libs

      Description

      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


        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: