2020你还不会Java8新特性? (20)

BaseStream 是所有流的父类 。

/** * 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} * @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 public interface Stream<T> extends BaseStream<T, Stream<T>> BaseStream(){ Iterator<T> iterator(); 迭代器 Spliterator<T> spliterator(); 分割迭代器 。 这是一个流的终止操作。 boolean isParallel(); 是否是并行。 S sequential(); // 返回一个等价的串行流。 返回S是一个新的流对象 S parallel(); //返回一个并行流。 S unordered(); // 返回一个无序的流。 S onClose(Runnable closeHandler); //当前流.onClose、 当close调用时,调用此方法。 void close(); // 关闭流 } 关闭处理器的举例 /** * 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. * * <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); public static void main(String[] args) { List<String> list = Arrays.asList("hello","world"); NullPointerException nullPointerException = new NullPointerException("myexception"); try (Stream<String> stream = list.stream()){ stream.onClose(()->{ System.out.println("aaa"); // throw new NullPointerException("first"); throw nullPointerException; }).onClose(()->{ System.out.println("aaa"); throw nullPointerException; }).forEach(System.out::println); } // 出现异常会被压制, // 如果是同一个异常对象,只会打印一次异常。 如果是多个异常对象。都会被打印。 }

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

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