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

Return value of average method from LongStream and LongSummaryStatistics

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      Hello Team,
       I am a java developer and do the job. Today I was reading LongStream and LongSummaryStatistics for OJP 11 certification. During the study, I see the discrepancy between average method of both classes.

      LongStream.average() returns OptionalDouble() containing the average element of the stream, or an empty optional if the stream is empty which is expected result
      while LongSummaryStatistics.getAverage() returns average double value of values or 0 if no element.

      My question is that why LongSummaryStatistics.getAverage() returns 0 if no element there . It should also return OptionalDouble as LongStream.average().

      Current logic creates the confusion why statistics class returning 0.0 in case of empty and like this we need to write to check two different logic. 0.0 can be valid value of average like we have two elements, 2 and -2 and average of both is 0.0.

      System.out.println(" 0.0/2 = "+ (0.0/2));
      Result :- 0.0/2 = 0.0

      In this case, how we can say stream was empty or not.

      Hope, You understand what I want to say here and team will look into this issue.

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              tongwan Andrew Wang
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: