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

Stream.flatMap(...).spliterator() cannot properly split after tryAdvance()

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Introduced In Version:
      8
    • Resolved In Build:
      b105
    • Verification:
      Verified

      Description

      Spliterator contract does not disable to split after advance. Though such possibility is never used in JDK, sometimes it's convenient to use it in third-party libraries. However spliterator produced by the stream which has upstream flatMap operation works incorrectly if trySplit() is called after tryAdvance(). Here's simple testcase:

      Spliterator<String> spltr = Stream.of("a", "b", "c").flatMap(x -> Stream.of(x, x)) // duplicate each stream element
      .parallel().spliterator();
      spltr.tryAdvance(System.out::println); // prints "a"
      Spliterator<String> prefix = spltr.trySplit(); // splits successfully
      prefix.forEachRemaining(System.out::println); // prints "b", "b"
      spltr.forEachRemaining(System.out::println); // prints "a", "c", "c"

      Unexpectedly the second "a" is printed by the suffix part and prefix skips over it. There's even existing test method SpliteratorTestHelper::testMixedTraverseAndSplit, which fails when such spliterator is supplied to it.

        Attachments

          Activity

            People

            • Assignee:
              tvaleev Tagir Valeev
              Reporter:
              tvaleev Tagir Valeev
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: