summaryStatistics Method of IntSummaryStatistics

前端 未结 4 1964
轮回少年
轮回少年 2021-01-23 17:27

Why summaryStatistics() method on an empty IntStream returns max and min value of integer as the maximum and minimum int value present in the stream?

IntStream          


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-23 17:42

    This behavior is documented in the Javadoc, so that's the correct behavior by definition:

    int java.util.IntSummaryStatistics.getMin()

    Returns the minimum value recorded, or Integer.MAX_VALUE if no values have been recorded.

    Returns:

    the minimum value, or Integer.MAX_VALUE if none

    and

    int java.util.IntSummaryStatistics.getMax()

    Returns the maximum value recorded, or Integer.MIN_VALUE if no values have been recorded.

    Returns:

    the maximum value, or Integer.MIN_VALUE if none

    As to "what's the point of returning these values", we can argue that the minimum value of an empty Stream should be such that if the Stream had any element, that element would be smaller than that value.

    Similarly, we can argue that the maximum value of an empty Stream should be such that if the Stream had any element, that element would be larger than that value.

提交回复
热议问题