2020了你还不会java8新特性?(八)流源构造代码分析与总结

记录下来,然后给别人再讲,你就掌握了。

学完之后忘记了怎么办?记录下来。 笔记 博客。 死记硬背是没有任何用的。

ReferencePipeline

/**
 * Abstract base class for an intermediate pipeline stage or pipeline source
 * stage implementing whose elements are of type {@code U}.
 */
//引用管道   
//ReferencePipeline  表示流的源阶段与中间阶段。
//ReferencePipeline.head表示流中的源阶段。
 abstract class ReferencePipeline
        extends AbstractPipeline>
        implements Stream  {  
 }

AbstractPipeline

/**
 * Abstract base class for "pipeline" classes, which are the core
 * implementations of the Stream interface and its primitive specializations.
 * Manages construction and evaluation of stream pipelines.
 *  
 * 

An {@code AbstractPipeline} represents an initial portion of a stream * pipeline, encapsulating a stream source and zero or more intermediate * operations. The individual {@code AbstractPipeline} objects are often * referred to as stages, where each stage describes either the stream * source or an intermediate operation. 流管道的初始的一部分。 * *

A concrete intermediate stage is generally built from an * {@code AbstractPipeline}, a shape-specific pipeline class which extends it * (e.g., {@code IntPipeline}) which is also abstract, and an operation-specific * concrete class which extends that. {@code AbstractPipeline} contains most of * the mechanics of evaluating the pipeline, and implements methods that will be * used by the operation; the shape-specific classes add helper methods for * dealing with collection of results into the appropriate shape-specific * containers. *避免自动拆箱和装箱操作。 *

After chaining a new intermediate operation, or executing a terminal * operation, the stream is considered to be consumed, and no more intermediate * or terminal operations are permitted on this stream instance. * 当链接完一个新的中间操作或者执行了终止操作之后, 这个流被认为被消费了。不允许再被操作了。 * @implNote *

For sequential streams, and parallel streams without * stateful intermediate * operations, parallel streams, pipeline evaluation is done in a single * pass that "jams" all the operations together. For parallel streams with * stateful operations, execution is divided into segments, where each * stateful operations marks the end of a segment, and each segment is * evaluated separately and the result used as the input to the next * segment. In all cases, the source data is not consumed until a terminal * operation begins. 只有终止操作开始的时候,源数据才会被消费。 * @param type of input elements * @param type of output elements * @param type of the subclass implementing {@code BaseStream} * @since 1.8 */ abstract class AbstractPipeline> extends PipelineHelper implements BaseStream { }

内部类,和lambda表达式之间的关系。

本质上 内部类和lambda不是一回事。只是能完成相同的操作。

lambda不是匿名内部类的语法糖,或者说是缩写。是一种新的形式。

public class LambdaTest {
    //内部类,和lambda表达式之间的关系。
    Runnable r1 = () -> System.out.println(this); // this表示当前类的对象

    //匿名内部类
    Runnable r2 = new Runnable() {  //
        @Override
        public void run() {
            System.out.println(this);
            // this 表示匿名内部类的对象
        }
    };


    public static void main(String[] args) {
        LambdaTest lambdaTest = new LambdaTest();

        Thread t1 = new Thread(lambdaTest.r1);
        t1.start();

        System.out.println("- - -- - ");

        Thread t2 = new Thread(lambdaTest.r2);
        t2.start();
        //输出结果。
        //com.sinosoft.lis.test.LambdaTest@62661526
        //com.sinosoft.lis.test.LambdaTest$1@59a30351
    }

}

使用了模板方法模式。

流是惰性的,是延迟操作的。遇到终止操作时,才会执行操作。

TerminalOp。 终止操作的接口类。

终止操作 只有四种类型, findOp foreachOp matchOp reduceOp

PipelineHelper

stream中间操作与终止操作层次体系分析与设计思想分析

中间操作

BaseStream -》 AbStractpipeline -》ReferencePipeline -》 Head || StatelessOP || statefulOp

最顶层的源 很多源的成员变量 管道 构造流源 无状态的中间操作 有状态的中间操作

流是惰性的,是延迟操作的。遇到终止操作时,才会执行操作。再没有终止操作之前,在整合中间操作(Sink)。

终止操作

TerminalOp -》 FindOp || ForeachOp || MatchOp || reduceOp

最顶层的

TerminalSink

终止的饮水槽。

你可能感兴趣的:(2020了你还不会java8新特性?(八)流源构造代码分析与总结)