average
OptionalDouble average()
Returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty. This is a special case of a reduction.
This is a terminal operation.
Returns:
an OptionalDouble containing the average element of this stream, or an empty optional if the stream is empty
max
OptionalInt max()
Returns an OptionalInt describing the maximum element of this stream, or an empty optional if this stream is empty. This is a special case of a reduction and is equivalent to:
return reduce(Integer::max);
This is a terminal operation.
Returns:
an OptionalInt containing the maximum element of this stream, or an empty OptionalInt if the stream is empty
min
OptionalInt min()
Returns an OptionalInt describing the minimum element of this stream, or an empty optional if this stream is empty. This is a special case of a reduction and is equivalent to:
return reduce(Integer::min);
This is a terminal operation.
Returns:
an OptionalInt containing the minimum element of this stream, or an empty OptionalInt if the stream is empty
sum
int sum()
Returns the sum of elements in this stream. This is a special case of a reduction and is equivalent to:
return reduce(0, Integer::sum);
This is a terminal operation.
Returns:
the sum of elements in this stream
summaryStatistics
IntSummaryStatistics summaryStatistics()
Returns an IntSummaryStatistics describing various summary data about the elements of this stream. This is a special case of a reduction.
This is a terminal operation.
Returns:
an IntSummaryStatistics describing various summary data about the elements of this stream
// streams/NumericStreamInfo.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import static streams.RandInts.*;
import java.util.stream.*;
public class NumericStreamInfo {
public static void main(String[] args) {
System.out.println(rands().average().getAsDouble());
System.out.println(rands().max().getAsInt());
System.out.println(rands().min().getAsInt());
System.out.println(rands().sum());
System.out.println(rands().summaryStatistics());
}
}
/* Output:
507.94
998
8
50794
IntSummaryStatistics{count=100, sum=50794, min=8, average=507.940000, max=998}
*/
The same operations are available for LongStreams and DoubleStreams.
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/NumericStreamInfo.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/RandInts.java
4. https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#average--
5. https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#max--
6. https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#sum--
7. https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html#summaryStatistics--
516

被折叠的 条评论
为什么被折叠?



