JAVA8学习——Stream底层的实现三(学习过程) (2)

接受了一个Sink对象,这个对象接受了操作的结果,并且返回了一个Sink,还会执行这个操作,并将这个结果传递给所提供的sink。 *(输入参数才是带结果的sinK)
正是因为这种操作,才能将sink给包装起来。

/** * Accepts a {@code Sink} which will receive the results of this operation, * and return a {@code Sink} which accepts elements of the input type of * this operation and which performs the operation, passing the results to * the provided {@code Sink}. 接受了一个Sink对象,这个对象接受了操作的结果,并且返回了一个Sink,还会执行这个操作,并将这个结果传递给所提供的sink。 *(输入参数才是带结果的sinK) 正式因为这种操作,才能将sink给包装起来。 * * @apiNote * The implementation may use the {@code flags} parameter to optimize the * sink wrapping. For example, if the input is already {@code DISTINCT}, * the implementation for the {@code Stream#distinct()} method could just * return the sink it was passed. * * @param flags The combined stream and operation flags up to, but not * including, this operation * @param sink sink to which elements should be sent after processing * @return a sink which accepts elements, perform the operation upon * each element, and passes the results (if any) to the provided * {@code Sink}. 参数本身是用来接收结果的,而不是用返回值来返回结果的。 */ abstract Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink); @Override final Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink) { throw new UnsupportedOperationException(); }

流的特性:惰性求值和延迟求值。

map()方法,包括其他的peek(),filter()等等中间操作的这些方法。只是完成了返回了一个StatelessOp对象。

所以中间操作返回一个终止对象可能执行的StatelessOp,没有终止操作,所以流不会被处理。

那么终止操作。我们要去追一下了。

拿代码中写的 forEach()方法开始去追

// Terminal operations from Stream @Override public void forEach(Consumer<? super P_OUT> action) { evaluate(ForEachOps.makeRef(action, false)); }

这是调用了makeRef()方法.方法在ForEachOps类中.

先看ForEachOps类的javadoc

/** * Factory for creating instances of {@code TerminalOp} that perform an * action for every element of a stream. Supported variants include unordered * traversal (elements are provided to the {@code Consumer} as soon as they are * available), and ordered traversal (elements are provided to the * {@code Consumer} in encounter order.) 这是一个工厂,用来创建 TerminalOp 对象,(终止操作。)这个对象会对每一个元素执行一个动作。 所支持的变化包括:无序的遍历,有序的遍历(按照所提供的的顺序来遍历)。 * * <p>Elements are provided to the {@code Consumer} on whatever thread and * whatever order they become available. For ordered traversals, it is * guaranteed that processing an element <em>happens-before</em> processing * subsequent elements in the encounter order. 元素被提供被一个任何可用的Consumer队形。 处理一个元素,一定是发生在 另外一件事之前 (happens-before)。 也就事 先遇到的元素先处理,后遇到的元素后处理。 * * <p>Exceptions occurring as a result of sending an element to the * {@code Consumer} will be relayed to the caller and traversal will be * prematurely terminated. 提供了大量的 静态方法。 * * @since 1.8 */ final class ForEachOps { }

如makeRef()

/** * Constructs a {@code TerminalOp} that perform an action for every element * of a stream. * * @param action the {@code Consumer} that receives all elements of a * stream * @param ordered whether an ordered traversal is requested * @param <T> the type of the stream elements * @return the {@code TerminalOp} instance */ public static <T> TerminalOp<T, Void> makeRef(Consumer<? super T> action, boolean ordered) { Objects.requireNonNull(action); return new ForEachOp.OfRef<>(action, ordered); }

TerminalOp说明

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgdfy.html