summaryStatistics Method of IntSummaryStatistics

前端 未结 4 1959
轮回少年
轮回少年 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:47

    To find a minimum int value in any array you typically initialize the default to 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. For max, the default max is initialized to Integer.MIN_VALUE.

提交回复
热议问题