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); }
/** * Returns a stream where the first element is the replaced with the result * of applying the given function while the other elements are left intact. * * <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 mapper a <a * href="package-summary.html#NonInterference">non-interfering </a>, * <a href="package-summary.html#Statelessness">stateless</a> * function to apply to the first element * @return the new stream * @since 0.4.1 */ public StreamEx<T> mapFirst(Function<? super T, ? extends T> mapper) { return supply(new PairSpliterator.PSOfRef<>(mapper, spliterator(), true)); }
/** * Returns a stream where the last element is the replaced with the result * of applying the given function while the other elements are left intact. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate * operation</a>. * * <p> * The mapper function is called at most once. It could be not called at all * if the stream is empty or there is short-circuiting operation downstream. * * @param mapper a <a * href="package-summary.html#NonInterference">non-interfering </a>, * <a href="package-summary.html#Statelessness">stateless</a> * function to apply to the last element * @return the new stream * @since 0.4.1 */ public StreamEx<T> mapLast(Function<? super T, ? extends T> mapper) { return supply(new PairSpliterator.PSOfRef<>(mapper, spliterator(), false)); }
/** * 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); }
/** * Creates an {@link EntryStream} consisting of the {@link Entry} objects * which keys are all the same and equal to the first element of this stream * and values are the rest elements of this stream. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate * operation</a>. * * <p> * The size of the resulting stream is one element less than the input * stream. If the input stream is empty or contains just one element, then * the output stream will be empty. * * @return the new stream * @see #withFirst(BiFunction) * @see #headTail(BiFunction) * @since 0.5.3 */ public EntryStream<T, T> withFirst() { WithFirstSpliterator<T, Entry<T, T>> spliterator = new WithFirstSpliterator<>(spliterator(), SimpleImmutableEntry::new); return new EntryStream<>(spliterator, context); }
/** * Returns a stream consisting of the results of applying the given function * to the the first element and every other element of this stream. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate * operation</a>. * * <p> * The size of the resulting stream is one element less than the input * stream. If the input stream is empty or contains just one element, then * 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 the first * stream element and every other element * @return the new stream * @see #withFirst() * @see #headTail(BiFunction) * @since 0.5.3 */ public <R> StreamEx<R> withFirst(BiFunction<? super T, ? super T, ? extends R> mapper) { WithFirstSpliterator<T, R> spliterator = new WithFirstSpliterator<>(spliterator(), mapper); return new StreamEx<>(spliterator, context); }
collection.addAll(list); } else { Spliterator<T> spltr = spliterator(); if (collection instanceof ArrayList) { long size = spltr.getExactSizeIfKnown();
/** * 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); }
/** * Returns a stream where the first element is transformed using * {@code firstMapper} function and other elements are transformed using * {@code notFirstMapper} function. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate * operation</a>. * * @param <R> The element type of the new stream element * @param firstMapper a <a * href="package-summary.html#NonInterference">non-interfering </a>, * <a href="package-summary.html#Statelessness">stateless</a> * function to apply to the first element * @param notFirstMapper a <a * href="package-summary.html#NonInterference">non-interfering </a>, * <a href="package-summary.html#Statelessness">stateless</a> * function to apply to all elements except the first one. * @return the new stream * @since 0.6.0 * @see #mapFirst(Function) */ public <R> StreamEx<R> mapFirstOrElse(Function<? super T, ? extends R> firstMapper, Function<? super T, ? extends R> notFirstMapper) { return new StreamEx<>(new PairSpliterator.PSOfRef<>(firstMapper, notFirstMapper, spliterator(), true), context); }
/** * Returns a stream where the last element is transformed using * {@code lastMapper} function and other elements are transformed using * {@code notLastMapper} function. * * <p> * This is a <a href="package-summary.html#StreamOps">quasi-intermediate * operation</a>. * * @param <R> The element type of the new stream element * @param notLastMapper a <a * href="package-summary.html#NonInterference">non-interfering </a>, * <a href="package-summary.html#Statelessness">stateless</a> * function to apply to all elements except the last one. * @param lastMapper a <a * href="package-summary.html#NonInterference">non-interfering </a>, * <a href="package-summary.html#Statelessness">stateless</a> * function to apply to the last element * @return the new stream * @since 0.6.0 * @see #mapFirst(Function) */ public <R> StreamEx<R> mapLastOrElse(Function<? super T, ? extends R> notLastMapper, Function<? super T, ? extends R> lastMapper) { return new StreamEx<>(new PairSpliterator.PSOfRef<>(lastMapper, notLastMapper, spliterator(), false), context); }
HeadTailSpliterator<T, R> spliterator = new HeadTailSpliterator<>(spliterator(), mapper, supplier); spliterator.context = context = context.detach(); return new StreamEx<>(spliterator, context);
return new EntryStream<>(new ZipSpliterator<>(spliterator(), other.spliterator(), SimpleImmutableEntry::new, true), context.combine(other));
return new StreamEx<>(new ZipSpliterator<>(spliterator(), other.spliterator(), mapper, true), context .combine(other));