首页 > 解决方案 > IntSummaryStatistics的summaryStatistics方法

问题描述

为什么空 IntStream 上的 summaryStatistics() 方法返回整数的最大值和最小值作为流中存在的最大和最小 int 值?

IntStream intStream = IntStream.of();
IntSummaryStatistics stat = intStream.summaryStatistics();
System.out.println(stat);  

输出

IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648}
  1. 返回这些值有什么意义?
  2. 它不应该考虑错误的结果吗?

标签: javajava-8functional-programmingjava-stream

解决方案


请参阅IntSummaryStatistics.getMax()IntSummaryStatistics.getMin()文档,当没有记录值时,此处描述了这种确切的行为。

在这些极端情况下返回这些是明智的。我将在这里解释这种行为IntSummaryStatistics.getMin()

示例:假设列表至少包含一个条目。无论该条目的整数值是什么,它都不会大于Integer.MAX_VALUE。因此,当没有条目时返回此值是有意义的,以便为用户提供洞察力。

同样适用IntSummaryStatistics.getMax()


推荐阅读