简体   繁体   English

IntSummaryStatistics的summaryStatistics方法

[英]summaryStatistics Method of IntSummaryStatistics

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 上的 summaryStatistics() 方法返回 integer 的最大值和最小值作为 stream 中存在的最大和最小 int 值?

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

Output : Output

IntSummaryStatistics{count=0, sum=0, min=2147483647, average=0.000000, max=-2147483648}
  1. What is the point of returning these values?返回这些值有什么意义?
  2. Should not it considered the wrong result?它不应该考虑错误的结果吗?

See IntSummaryStatistics.getMax() and IntSummaryStatistics.getMin() docs, this exact behaviour is described there, when no values are recorded.请参阅IntSummaryStatistics.getMax()IntSummaryStatistics.getMin()文档,当没有记录值时,此处描述了这种确切的行为。

It is just sensible to return these in these corner cases.在这些极端情况下返回这些是明智的。 I will just explain this behaviour IntSummaryStatistics.getMin() here.我将在这里解释这种行为IntSummaryStatistics.getMin()

Example: Let us just say the list contains at least one entry.示例:假设列表至少包含一个条目。 Whatever the integer value is for that entry, it is not going to be greater than Integer.MAX_VALUE .无论该条目的 integer 值是什么,它都不会大于Integer.MAX_VALUE So returning this value when there are no entries makes sense, to give an insight to the user.因此,当没有条目时返回此值是有意义的,以便为用户提供洞察力。

Same goes for IntSummaryStatistics.getMax() IntSummaryStatistics.getMax()也是如此

This behavior is documented in the Javadoc, so that's the correct behavior by definition:此行为记录在 Javadoc 中,因此根据定义这是正确的行为:

int java.util.IntSummaryStatistics.getMin() int java.util.IntSummaryStatistics.getMin()

Returns the minimum value recorded, or Integer.MAX_VALUE if no values have been recorded.返回记录的最小值,如果没有记录值,则返回 Integer.MAX_VALUE。

Returns:回报:

the minimum value, or Integer.MAX_VALUE if none最小值,如果没有,则为 Integer.MAX_VALUE

and

int java.util.IntSummaryStatistics.getMax() int java.util.IntSummaryStatistics.getMax()

Returns the maximum value recorded, or Integer.MIN_VALUE if no values have been recorded.返回记录的最大值,如果没有记录任何值,则返回 Integer.MIN_VALUE。

Returns:回报:

the maximum value, or Integer.MIN_VALUE if none最大值,如果没有,则为 Integer.MIN_VALUE

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.至于“返回这些值的意义何在”,我们可以争辩说,空Stream的最小值应该是这样的,即如果Stream有任何元素,则该元素将小于该值。

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.同样,我们可以争辩说,空Stream的最大值应该是这样的,即如果Stream有任何元素,则该元素将大于该值。

To find a minimum int value in any array you typically initialize the default to Integer.MAX_VALUE .要在任何数组中找到minimum int值,通常将默认值初始化为Integer.MAX_VALUE

int min = Integer.MAX_VALUE;

for (int i : somearray) {
   if (i < min) {
     min = i;
   }
}

return min;  

So if there was nothing to iterate, the value of the default min would be returned.因此,如果没有要迭代的内容,则将返回默认min的值。 For max, the default max is initialized to Integer.MIN_VALUE .对于最大值,默认max初始化为Integer.MIN_VALUE

Since the list is empty and those methods return an actual value and not an Optional<Integer> it assumes the full Integer range.由于列表为空并且这些方法返回一个实际值而不是Optional<Integer>它假定完整的 Integer 范围。 Otherwise it would return an empty Optional<Integer> .否则它将返回一个空的Optional<Integer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM