版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者。
https://blog.csdn.net/weixin_43453386/article/details/88668714
流的生成方式,请参考 https://blog.csdn.net/weixin_43453386/article/details/88643166
以下三种流都是在java.util.stream空间下
序号 | 类型 | 类 | 说明 |
---|---|---|---|
1 | int | IntStream | 原始整数值序列 |
2 | long | LongStream | 原始长整型值序列 |
3 | double | DoubleStream | 原始双精度浮点型序列 |
一、常用方法
序号 | 方法 | 说明 |
---|---|---|
1 | rangeClosed(a,b) | 返回子序列 [a,b],左闭右闭(包括 b 元素,增长步值为 1) |
2 | range(a,b) | 返回子序列 [a,b),左闭右开(不包括 b) |
3 | sum | 计算总和 |
4 | sorted | 排序 |
5 | toArray | 流转为数组 |
6 | max | 最大值 |
7 | min | 最小值 |
8 | average | 平均值 |
二、示例
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class T {
public static void main(String[] args) {
System.out.println("**********IntStream start**********");
//12345
IntStream.rangeClosed(1, 5).forEach(e->System.out.print(e));
System.out.println();
//1234
IntStream.range(1,5).forEach(e->System.out.print(e));
System.out.println();
//1+2+3+4+5=15
System.out.println(IntStream.rangeClosed(1,5).sum());
//2 4 8 13 15
IntStream.of(13,4,15,2,8).sorted().forEach(s->System.out.println(s ));
System.out.println(IntStream.of(13,4,15,2,8).toArray());
System.out.println("**********IntStream end**********");
System.out.println("**********LongStream start**********");
//12345
LongStream.rangeClosed(1, 5).forEach(e->System.out.print(e));
System.out.println();
//1234
LongStream.range(1,5).forEach(e->System.out.print(e));
System.out.println();
//1+2+3+4+5=15
System.out.println(IntStream.rangeClosed(1,5).sum());
//2 4 8 13 15
LongStream.of(13,4,15,2,8).sorted().forEach(s->System.out.println(s ));
System.out.println("**********LongStream end**********");
System.out.println("**********DoubleStream start**********");
//13.3
System.out.println(DoubleStream.of(12.2,11.1,13.3).max().getAsDouble());
//11.1
System.out.println(DoubleStream.of(12.2,11.1,13.3).min().getAsDouble());
//12.20
System.out.println(DoubleStream.of(12.2,11.1,13.3).average().getAsDouble());
System.out.println("**********DoubleStream end**********");
}
}