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

Stream底层的实现

Stream接口实现了 BaseStream 接口,我们先来看看BaseStream的定义

BaseStream

BaseStream是所有流的父类接口。

对JavaDoc做一次解读,了解提供的所有方法。 /** * Base interface for streams, which are sequences of elements supporting * sequential and parallel aggregate operations. The following example * illustrates an aggregate operation using the stream types {@link Stream} * and {@link IntStream}, computing the sum of the weights of the red widgets: * * <pre>{@code * int sum = widgets.stream() * .filter(w -> w.getColor() == RED) * .mapToInt(w -> w.getWeight()) * .sum(); * }</pre> * * See the class documentation for {@link Stream} and the package documentation * for <a href="http://www.likecs.com/package-summary.html">java.util.stream</a> for additional * specification of streams, stream operations, stream pipelines, and * parallelism, which governs the behavior of all stream types. * * @param <T> the type of the stream elements * @param <S> the type of of the stream implementing {@code BaseStream} S 代表中间操作产生的新的流操作。 * @since 1.8 * @see Stream * @see IntStream * @see LongStream * @see DoubleStream * @see <a href="http://www.likecs.com/package-summary.html">java.util.stream</a> */ public interface BaseStream<T, S extends BaseStream<T, S>> extends AutoCloseable { /** * Returns an iterator for the elements of this stream. * * <p>This is a <a href="package-summary.html#StreamOps">terminal * operation</a>. * * @return the element iterator for this stream */ Iterator<T> iterator(); //迭代器 ,针对于流中元素类型*(T)的迭代器 /** * Returns a spliterator for the elements of this stream. * * <p>This is a <a href="package-summary.html#StreamOps">terminal * operation</a>. * * @return the element spliterator for this stream */ Spliterator<T> spliterator(); //分割迭代器, 流中的核心的操作。 /** * Returns whether this stream, if a terminal operation were to be executed, * would execute in parallel. Calling this method after invoking an * terminal stream operation method may yield unpredictable results. * * @return {@code true} if this stream would execute in parallel if executed */ boolean isParallel(); //是否并行 /** * Returns an equivalent stream that is sequential. May return * itself, either because the stream was already sequential, or because * the underlying stream state was modified to be sequential.,. 返回一个等价的串行流,有可能返回流本身,或者是流修改成串行流的 * * <p>This is an <a href="package-summary.html#StreamOps">intermediate * operation</a>. * * @return a sequential stream */ S sequential(); //返回值为S:流,新的流对象 /** * Returns an equivalent stream that is parallel. May return * itself, either because the stream was already parallel, or because * the underlying stream state was modified to be parallel. * * <p>This is an <a href="package-summary.html#StreamOps">intermediate * operation</a>. * * @return a parallel stream */ S parallel(); /** * Returns an equivalent stream that is * <a href="package-summary.html#Ordering">unordered</a>. May return * itself, either because the stream was already unordered, or because * the underlying stream state was modified to be unordered. * * <p>This is an <a href="package-summary.html#StreamOps">intermediate * operation</a>. * * @return an unordered stream */ S unordered(); /** * Returns an equivalent stream with an additional close handler. Close * handlers are run when the {@link #close()} method * is called on the stream, and are executed in the order they were * added. All close handlers are run, even if earlier close handlers throw * exceptions. If any close handler throws an exception, the first * exception thrown will be relayed to the caller of {@code close()}, with * any remaining exceptions added to that exception as suppressed exceptions * (unless one of the remaining exceptions is the same exception as the * first exception, since an exception cannot suppress itself.) May * return itself. 返回值为流。流中带了一个关闭处理器、关闭处理器调用的是 close()方法。 按照被添加的顺序去关闭。 * * <p>This is an <a href="package-summary.html#StreamOps">intermediate * operation</a>. * * @param closeHandler A task to execute when the stream is closed * @return a stream with a handler that is run if the stream is closed */ S onClose(Runnable closeHandler); /** * Closes this stream, causing all close handlers for this stream pipeline * to be called. * * @see AutoCloseable#close() */ @Override void close(); } 对onClose关闭处理器做单独的认识 public class StreamTest2 { public static void main(String[] args) { List<String> list = Arrays.asList("hello", "world", "hello world"); // list.stream().onClose(()-> System.out.println("aaa")).onClose(()-> System.out.println("bbb")).forEach(System.out::println); try (Stream<String> stream = list.stream()){ stream.onClose(()-> { System.out.println("aaa"); throw new NullPointerException("first Exception"); }).onClose(()->{ System.out.println("bbb"); throw new ArithmeticException("first Exception"); }).forEach(System.out::println); } } } Exception in thread "main" java.lang.NullPointerException: first Exception at com.dawa.jdk8.StreamTest2.lambda$main$0(StreamTest2.java:21) at java.util.stream.Streams$1.run(Streams.java:850) at java.util.stream.AbstractPipeline.close(AbstractPipeline.java:323) at com.dawa.jdk8.StreamTest2.main(StreamTest2.java:26) Suppressed: java.lang.ArithmeticException: first Exception at com.dawa.jdk8.StreamTest2.lambda$main$1(StreamTest2.java:24) at java.util.stream.Streams$1.run(Streams.java:854) ... 2 more

几种可能的情况

//运行结果就可以看到 aa,bbb被调用。

//也可以看到压制异常

//如果两个地方的异常是相同的异常对象,则第二个异常不会被压制。因为异常是自己不会压制自己的。

//如果是同一种异常,但是不是同一个异常,还是会压制的。

备注:遇到问题,javadoc里面已经写的很清楚了。往往每个人都伸手可得的内容,容易最被忽视掉。

Stream源码分析

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

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