一 .概述
在jdk8之中提出了stream的概念,通过stream的操作可以完成对集合和数组的计算操作.
我们使用stream的步骤基本上可以分成三个部分.
[1]创建Stream
[2]中间操作
[3]终止操作
二 . 创建stream
在jdk之中提供了四种创建Stream的方式:
[1]在Collection之中提供了一个公共的方法
eg:
Stream strem = new ArrayList<String>().stream();
因此对于一个集合来说,我们都可以使用Stream的方式进行操作.
[2]通过Arrays的stream()方法将一个数组转换为一个stream对象
eg:
Arrays.stream();
[3]通过Stream的静态方法of()方法创建stream.
eg:
Stream<String> stream = Stream.of(new String[] {"1","ff","sds","sjd"});
[4]使用无线流
方式一: 使用iterate
Stream.iterate(0, x -> x+1).limit(3).forEach(System.out::println);
方式二:
eg:
Stream.generate(()->new Random().nextInt(12)) .limit(3).forEach(System.out::println);