streams - Information for Numeric Streams

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--

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值