Stream实现了对数据源的流式处理,它可以并行操作,提高数据处理效率。什么是流流不是集合,它不对数据做保存,只是最数据进行算法处理,比如最大值,最小值,排序等操作。Stream会在数据源内部隐式的遍历进行处理。Stream会并行遍历数据,将数据分成若干段,同时进行处理,最终汇总结果一起输出。Stream 就如同一个迭代器(Iterator),单向,不可往复,数据只能遍历一次,遍历过一次后即用尽了,就好比流水从面前流过,一去不复返。特点首先对stream的操作可以分为两类,中间操作(intermediate operations)和结束操作(terminal operations):中间操作总是会惰式执行,调用中间操作只会生成一个标记了该操作的新stream。结束操作会触发实际计算,计算发生时会把所有中间操作积攒的操作以pipeline的方式执行,这样可以减少迭代次数。计算完成之后stream就会失效。无存储。stream不是一种数据结构,它只是某种数据源的一个视图,数据源可以是一个数组,Java容器或I/O channel等。为函数式编程而生。对stream的任何修改都不会修改背后的数据源,比如对stream执行过滤操作并不会删除被过滤的元素,而是会产生一个不包含被过滤元素的新stream。惰式执行。stream上的操作并不会立即执行,只有等到用户真正需要结果的时候才会执行。可消费性。stream只能被“消费”一次,一旦遍历过就会失效,就像容器的迭代器那样,想要再次遍历必须重新生成。使用方法1.构造流的方法public class StreamStudy { public static void main(String[] args) throws Exception { //1. of Stream<String> stream = Stream.of(“hello”,“java”,“python”); // 2. Arrays String [] strArray = new String[] {“hello”,“java”,“python”}; stream = Stream.of(strArray); stream = Arrays.stream(strArray); // 3. Collections List<String> list = Arrays.asList(strArray); stream = list.stream(); System.out.println(stream.findAny()); }}最终只返回第一个结果: Optional[hello]2. 流转换为其它数据结构public class StreamStudy { public static void main(String[] args) throws Exception { String [] strArray = new String[] {“hello”,“java”,“python”}; List<String> list = Arrays.asList(strArray); // to array System.out.println(list.stream().toArray()[0]); // to list System.out.println(list.stream().collect(Collectors.toList())); // to string System.out.println(list.stream().collect(Collectors.joining()).toString()); }}输出:hello[hello, java, python]hellojavapython3. 流的操作Intermediate (map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered)Terminal(forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator)Short-circuiting(anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 limit)一个流可以后面跟随零个或多个 intermediate 操作。其目的主要是打开流,做出某种程度的数据映射/过滤,然后返回一个新的流,交给下一个操作使用。这类操作都是惰性化的(lazy),就是说,仅仅调用到这类方法,并没有真正开始流的遍历。一个流只能有一个 terminal 操作,当这个操作执行后,流就被使用“光”了,无法再被操作。所以这必定是流的最后一个操作。Terminal 操作的执行,才会真正开始流的遍历,并且会生成一个结果,或者一个 side effect。4.基础的使用1.map+forEachpublic class StreamStudy { public static void main(String[] args) throws Exception { String [] strArray = new String[] {“hello”,“java”,“python”}; List<String> list = Arrays.asList(strArray); list.stream().map((v) ->v.toUpperCase()) .forEach(t -> System.out.println(t)); }}将list中的所有字母转换成大写,然后遍历输出。 实际list中的值并没有改变,我们只是借助Stream来做业务处理。输出 :HELLOJAVAPYTHON2.filter+map+forEachpublic class StreamStudy { public static void main(String[] args) throws Exception { String [] strArray = new String[] {“hello”,“java”,“python”}; List<String> list = Arrays.asList(strArray); list.stream().filter(f -> f.length()>4) .map(v ->v.toUpperCase()) .forEach(t -> System.out.println(t)); }}先filter过滤,然后map字母大写,最后forEach输出结果:HELLOPYTHON3. filter+sorted+map+forEachpublic class StreamStudy { public static void main(String[] args) throws Exception { String [] strArray = new String[] {“hello”,“java”,“python”,“node”,“react”,“vue”}; List<String> list = Arrays.asList(strArray); list.stream().filter(f -> f.length()>3) .sorted((a,b) -> b.compareTo(a)) .map(v ->v.toUpperCase()) .forEach(t -> System.out.println(t)); }}先filter过滤,然后sorted排序,然后map字母大写,最后forEach输出结果:REACTPYTHONNODEJAVAHELLO4. filter+sorted+map+distinct+forEachpublic class StreamStudy { public static void main(String[] args) throws Exception { String [] strArray = new String[] {“hello”,“java”,“python”,“node”,“react”,“vue”,“React”}; List<String> list = Arrays.asList(strArray); list.stream().filter(f -> f.length()>3) .sorted((a,b) -> b.compareTo(a)) .map(v ->v.toUpperCase()) .distinct() .forEach(t -> System.out.println(t)); }}distinct去重,使用 Object.equals(Object)来判断是否重复,最终只留下一个REACT,结果:REACTPYTHONNODEJAVAHELLO5. filter+sorted+map+distinct+limit+forEachpublic class StreamStudy { public static void main(String[] args) throws Exception { String [] strArray = new String[] {“hello”,“java”,“python”,“node”,“react”,“vue”,“React”}; List<String> list = Arrays.asList(strArray); list.stream().filter(f -> f.length()>3) .sorted((a,b) -> b.compareTo(a)) .map(v ->v.toUpperCase()) .distinct() .limit(3) .forEach(t -> System.out.println(t)); }}limit限制元素个数,这里着要前3个结果:REACTPYTHONNODE6.filter+sorted+map+distinct+limit+peek+forEachpublic class StreamStudy { public static void main(String[] args) throws Exception { String [] strArray = new String[] {“hello”,“java”,“python”,“node”,“react”,“vue”,“React”}; List<String> list = Arrays.asList(strArray); list.stream().filter(f -> f.length()>3) .sorted((a,b) -> b.compareTo(a)) .map(v ->v.toUpperCase()) .distinct() .limit(3) .peek(p -> p.toLowerCase()) .forEach(t -> System.out.println(t)); }}peek会对每个元素执行操作,并返回包含原stream元素的新Stream,什么意思呢?先看结果:REACTPYTHONNODE并不是我们看到的小写,因为peek产生的新的stream并不是我们已开始处理的Stream,所以我们看到的还是大写。如果你的处理过程中涉及一些额外逻辑,但不影响最终结果,那么你可以使用peek去搞一个新的Stream去处理。7.总结我们主要使用的是Intermediate 中的方法进行数据处理,Terminal 中的方法只能使用一个,这意味着对流的处理终止,这时才开始执行前面的那些Intermediate方法。最后对一些方法作一些解释,就不一一演示了:forEach遍历、 forEachOrdered按顺序遍历、 toArray结果转换成数组、 reduce结果中的元素进行组合、 collect结果转换成集合、 min结果中最小值、 max结果中最大值、 count结果中元素数量、 anyMatch结果中存在元素满足某一条件、 allMatch结果中所有元素都满足某一条件、 noneMatch结果中所有元素都不满足某一条件、 findFirst结果中第一条数据 、 findAny结果中的任意一条数据、 iterator遍历欢迎关注我的公众号mike啥都想搞,有更多教程资料相送。