BaseStream 和 Stream
1 // Base interface for streams, which are sequences of elements supporting 2 // sequential and parallel aggregate operations. 3 // @param <T> the type of the stream elements 4 // @param <S> the type of of the stream implementing {@code BaseStream} 5 public interface BaseStream<T, S extends BaseStream<T, S>> { 6 7 // Returns an iterator for the elements of this stream. 8 Iterator<T> iterator(); 9 10 // Returns a spliterator for the elements of this stream. 11 Spliterator<T> spliterator(); 12 13 // Returns whether this stream, if a terminal operation were to be executed, 14 // would execute in parallel. 15 boolean isParallel(); 16 17 // Returns an equivalent stream that is sequential. 18 S sequential(); 19 20 // Returns an equivalent stream that is parallel. 21 S parallel(); 22 23 // Returns an equivalent stream that is unordered. 24 S unordered(); 25 26 // Returns an equivalent stream with an additional close handler. 27 S onClose(Runnable closeHandler); 28 }
BaseStream 作为java Stream处理里面最顶级api,为串行和并行进行定义。
Iterator迭代器
spliterator可分割迭代器
parallel并行
sequential串行
unordered无序的
// @param <T> the type of the stream elements public interface Stream<T> extends BaseStream<T, Stream<T>> { /** * Returns a stream consisting of the elements of this stream that match * the given predicate. * <p>This is an intermediate operation. * @return the new stream. */ Stream<T> filter(Predicate<? super T> predicate); /** * Returns a stream consisting of the results of applying the given * function to the elements of this stream. * This is an intermediate operation. * @param <R> The element type of the new stream. * @param mapper a non-interfering stateless function * to apply to each element. * @return the new stream. */ <R> Stream<R> map(Function<? super T, ? extends R> mapper); /** * Returns a stream consisting of the distinct elements (according to * {@link Object#equals(Object)}) of this stream. * <p>This is a stateful intermediate operation. * @return the new stream */ Stream<T> distinct(); /** * Returns a stream consisting of the elements of this stream, sorted * according to natural order. * <p>For ordered streams, the sort is stable. For unordered streams, no * stability guarantees are made. * <p>This is a <a href="package-summary.html#StreamOps">stateful * intermediate operation</a>. * * @return the new stream
*/ Stream<T> sorted(); /** * Returns a stream consisting of the elements of this stream, sorted * according to the provided {@code Comparator}. * * @return the new stream
*/ Stream<T> sorted(Comparator<? super T> comparator); /** * Returns a stream consisting of the elements of this stream, additionally * performing the provided action on each element as elements are consumed * from the resulting stream. * * This is an intermediate operation. * * <p>For parallel stream pipelines, the action may be called at * whatever time and in whatever thread the element is made available by the * upstream operation. If the action modifies shared state, * it is responsible for providing the required synchronization. * @return the new stream */ Stream<T> peek(Consumer<? super T> action); /** * Returns a stream consisting of the remaining elements of this stream * after discarding the first {@code n} elements of the stream. * If this stream contains fewer than {@code n} elements then an * empty stream will be returned. * * <p>This is a <a href="package-summary.html#StreamOps">stateful * intermediate operation</a>. * @param n the number of leading elements to skip * @return the new stream * @throws IllegalArgumentException if {@code n} is negative */ Stream<T> skip(long n); // ----------------------------------------------------- }
上面我只是把部分中间操作intermediate operation的方法拿了出来。而这些操作都会返回一个新的Stream。
public interface Stream<T> extends BaseStream<T, Stream<T>> { /** * Performs an action for each element of this stream. * * <p>This is a <a href="package-summary.html#StreamOps">terminal * operation</a>. */ void forEach(Consumer<? super T> action); void forEachOrdered(Consumer<? super T> action); // return an array containing the elements of this stream Object[] toArray(); <A> A[] toArray(IntFunction<A[]> generator); T reduce(T identity, BinaryOperator<T> accumulator); <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner); <R, A> R collect(Collector<? super T, A, R> collector); Optional<T> min(Comparator<? super T> comparator); Optional<T> max(Comparator<? super T> comparator); }
并且也提供了forEach reduce min max collect等终端操作terminal operation。
并且提供了一些静态的非抽象方法。