Tuesday, April 20, 2021

Java SE 8+ STREAM: peek() vs. forEach() etc.


Sources:


https://programuj.pl/blog/java8-strumienie-cz3-peek-map-collect


<R,A> Rcollect(Collector<? super T,A,R> collector)
Performs a mutable reduction operation on the elements of this stream using a Collector.
<R> Rcollect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
Performs a mutable reduction operation on the elements of this stream.


Stream<T>filter(Predicate<? super T> predicate)
Returns a stream consisting of the elements of this stream that match the given predicate.

static <T> Stream<T>generate(Supplier<T> s)
Returns an infinite sequential unordered stream where each element is generated by the provided Supplier.
static <T> Stream<T>iterate(T seed, UnaryOperator<T> f)
Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seedf(seed)f(f(seed)), etc.
Stream<T>limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
<R> Stream<R>map(Function<? super T,? extends R> mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream.

static <T> Stream<T>of(T... values)
Returns a sequential ordered stream whose elements are the specified values.
static <T> Stream<T>of(T t)
Returns a sequential Stream containing a single element.


Optional<T>reduce(BinaryOperator<T> accumulator)
Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
Treduce(T identity, BinaryOperator<T> accumulator)
Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
<U> Ureduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions.


<R> Stream<R>flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
Returns a stream 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.

voidforEach(Consumer<? super T> action)
Performs an action for each element of this stream.

Stream<T>peek(Consumer<? super T> action)
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.



No comments: