@Override StreamEx<T> supply(Stream<T> stream) { return new StreamEx<>(stream, context); }
@Override StreamEx<T> supply(Spliterator<T> spliterator) { return new StreamEx<>(spliterator, context); }
/** * Returns a sequential {@link StreamEx} created from given * {@link Spliterator}. * * @param <T> the type of stream elements * @param spliterator a spliterator to create the stream from. * @return the new stream * @since 0.3.4 */ public static <T> StreamEx<T> of(Spliterator<? extends T> spliterator) { return new StreamEx<>(spliterator, StreamContext.SEQUENTIAL); }
@Override public StreamEx<Integer> boxed() { return new StreamEx<>(stream().boxed(), context); }
@Override public <U> StreamEx<U> mapToObj(IntFunction<? extends U> mapper) { return new StreamEx<>(stream().mapToObj(mapper), context); }
@Override public <R> StreamEx<R> map(Function<? super T, ? extends R> mapper) { return new StreamEx<>(stream().map(mapper), context); }
/** * Returns an infinite sequential unordered {@code StreamEx} where each * element is generated by the provided {@link Supplier}. This is suitable * for generating constant streams, streams of random elements, etc. * * @param <T> the type of stream elements * @param s the {@code Supplier} of generated elements * @return a new infinite sequential unordered {@code StreamEx} * @see Stream#generate(Supplier) * @see EntryStream#generate(Supplier, Supplier) */ public static <T> StreamEx<T> generate(Supplier<T> s) { return new StreamEx<>(Stream.generate(s), StreamContext.SEQUENTIAL); }
@Override public <R> StreamEx<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) { return new StreamEx<>(stream().flatMap(mapper), context); }
/** * Returns a {@link StreamEx} consisting of the results of replacing each * element of this stream with the contents of a mapped stream produced by * applying the provided mapping function to each element. Each mapped * stream is closed after its contents have been placed into this stream. * (If a mapped stream is {@code null} an empty stream is used, instead.) * * <p> * 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 which produces a {@code Stream} of new values * @return the new stream * @since 0.3.0 */ public <R> StreamEx<R> flatMapToObj(IntFunction<? extends Stream<R>> mapper) { return new StreamEx<>(stream().mapToObj(mapper).flatMap(Function.identity()), context); }
/** * Returns an {@link StreamEx} object which wraps given {@link Stream}. * * <p> * The supplied stream must not be consumed or closed when this method is * called. No operation must be performed on the supplied stream after it's * wrapped. * * @param <T> the type of stream elements * @param stream original stream * @return the wrapped stream */ public static <T> StreamEx<T> of(Stream<T> stream) { if (stream instanceof AbstractStreamEx) { @SuppressWarnings("unchecked") AbstractStreamEx<T, ?> ase = (AbstractStreamEx<T, ?>) stream; if (ase.spliterator != null) return new StreamEx<>(ase.spliterator(), ase.context); return new StreamEx<>(ase.stream(), ase.context); } return new StreamEx<>(stream, StreamContext.of(stream)); }
/** * Returns a {@link StreamEx} consisting of the results of replacing each * element of this stream with the contents of a mapped stream produced by * applying the provided mapping function to each element. Each mapped * stream is closed after its contents have been placed into this stream. * (If a mapped stream is {@code null} an empty stream is used, instead.) * * <p> * 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 which produces a {@code Stream} of new values * @return the new stream * @since 0.3.0 */ public <R> StreamEx<R> flatMapToObj(DoubleFunction<? extends Stream<R>> mapper) { return new StreamEx<>(stream().mapToObj(mapper).flatMap(Function.identity()), context); }
/** * Returns a {@link StreamEx} consisting of the results of replacing each * element of this stream with the contents of a mapped stream produced by * applying the provided mapping function to each element. Each mapped * stream is closed after its contents have been placed into this stream. * (If a mapped stream is {@code null} an empty stream is used, instead.) * * <p> * 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 which produces a {@code Stream} of new values * @return the new stream * @since 0.3.0 */ public <R> StreamEx<R> flatMapToObj(LongFunction<? extends Stream<R>> mapper) { return new StreamEx<>(stream().mapToObj(mapper).flatMap(Function.identity()), context); }
@Override public StreamEx<Double> boxed() { return new StreamEx<>(stream().boxed(), context); }
@Override public <U> StreamEx<U> mapToObj(LongFunction<? extends U> mapper) { return new StreamEx<>(stream().mapToObj(mapper), context); }
private <R> StreamEx<R> collapseInternal(BiPredicate<? super T, ? super T> collapsible, Function<T, R> mapper, BiFunction<R, T, R> accumulator, BinaryOperator<R> combiner) { CollapseSpliterator<T, R> spliterator = new CollapseSpliterator<>(collapsible, mapper, accumulator, combiner, spliterator()); return new StreamEx<>(spliterator, context); }
@Override public <U> StreamEx<U> mapToObj(DoubleFunction<? extends U> mapper) { return new StreamEx<>(stream().mapToObj(mapper), context); }
@Override public StreamEx<Long> boxed() { return new StreamEx<>(stream().boxed(), context); }
/** * Returns a stream consisting of the results of applying the given function * to the every adjacent pair of elements of this stream. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate</a> * operation. * * <p> * The output stream will contain one element less than this stream. If this * stream contains zero or one element the output stream will be empty. * * @param <R> The element type of the new stream * @param mapper a non-interfering, stateless function to apply to each * adjacent pair of this stream elements. * @return the new stream * @since 0.2.1 */ public <R> StreamEx<R> pairMap(BiFunction<? super T, ? super T, ? extends R> mapper) { PSOfRef<T, R> spliterator = new PairSpliterator.PSOfRef<>(mapper, spliterator()); return new StreamEx<>(spliterator, context); }
/** * Return a new {@link StreamEx} containing all the nodes of tree-like data * structure in depth-first order. * * <p> * The streams created by mapper may be automatically * {@link java.util.stream.BaseStream#close() closed} after its contents * already consumed and unnecessary anymore. It's not guaranteed that all * created streams will be closed during the stream terminal operation. If * it's necessary to close all the created streams, call the {@code close()} * method of the resulting stream returned by {@code ofTree()}. * * @param <T> the type of tree nodes * @param root root node of the tree * @param mapper a non-interfering, stateless function to apply to each tree * node which returns null for leaf nodes or stream of direct * children for non-leaf nodes. * @return the new sequential ordered stream * @since 0.2.2 * @see EntryStream#ofTree(Object, BiFunction) * @see #ofTree(Object, Class, Function) */ public static <T> StreamEx<T> ofTree(T root, Function<T, Stream<T>> mapper) { TreeSpliterator<T, T> spliterator = new TreeSpliterator.Plain<>(root, mapper); return new StreamEx<>(spliterator, StreamContext.SEQUENTIAL.onClose(spliterator::close)); }
/** * Returns a new {@code StreamEx} which is a concatenation of supplied value * and this stream. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate * operation</a> with <a href="package-summary.html#TSO">tail-stream * optimization</a>. * * @param value the value to prepend to the stream * @return the new stream * @since 0.5.4 */ public StreamEx<T> prepend(T value) { return new StreamEx<>(new PrependSpliterator<>(spliterator(), value), context); }