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

clarify specification of Stream.count()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • None
    • core-libs
    • None

      The change JDK-8067969 allowed Stream.count() to short-circuit if the size of the stream is known.

      The specification for Stream.count() says:

           * Returns the count of elements in this stream. This is a special case of
           * a reduction and is equivalent to:
           *
           * return mapToLong(e -> 1L).sum();

      [markup elided for clarity]

      This makes it sound as if count() is defined in terms of mapToLong(e -> 1L).sum(). However, the @apiNote below describes the possibility of short-circuiting which seems to contradict the above.

      It would probably be best if mapToLong(e -> 1L).sum() were removed from the specification portion of this method.

      This would be a useful example to show in the case where the programmer explicitly wants to avoid the short-circuiting behavior, for example, if there is a peek() with side effects upstream in the pipeline.

            psandoz Paul Sandoz
            smarks Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: