Java 8 Stream Operations

Java 8 Streams API supports parallel operations to process your data, while completely abstracting out the low level multi-threading logic. This logic allows the developer to fully concentrate on the data and the operations to be performed on the data.

Stream operations are divided into terminal and intermediate type operations, and are combined to form stream pipelines. What is a stream pipeline? A stream pipeline consists of a source, like a collection, array, or I/O channel, which is followed by zero or more intermediate operations such as Stream.

The Java 8 Stream Operations listed below are the most widely used stream operations.

forEach() Operation

The forEach() stream operation performs an action for each element of the stream. See a working Java 8 Stream forEach() method example.

void forEach(Consumer<? super T> action)


count() Operation

The count() stream operation will the count of equal to the number of elements in a stream. Count() is a terminal operation, meaning it may traverse the stream to produce a result or a side-effect. Check out a working sample that uses the count operation.

long count()


distinct() Operation

The distinct() stream operation will return a stream consisting of the distinct elements of the stream.

Stream<T> distinct()


map() Operation

The map() stream operation will return a stream consisting of the results of applying the given function to the elements of this stream. See a working Java 8 Stream map() method example.

<R> Stream<R> map(Function<? super T,? extends R> mapper)


collect() Operation

The collect() stream operation performs a mutable reduction operation on the elements of this stream using a Collector. The collect() operation has two different method signatures. See some working Java 8 Stream collect() method examples.

<R,A> R collect(Collector<? super T,A,R> collector)
.....
<R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)


filter() Operation

The filter() stream operation will return a stream consisting of the elements of this stream that match the given predicate. See some working Java 8 Stream filter() method examples.

Stream<T> filter(Predicate<? super T> predicate)


findFirst() Operation

The firdFirst() stream operation will return an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, any element may be returned. See some working Java 8 Stream findFirst() method examples.

Optional<T> findFirst()


findAny() Operation

The firdAny() stream operation will return an Optional describing some element of this stream, or an empty Optional if the stream is empty. If doing a non-parallel operation, the method will most likely return the first element in the Stream. See some working Java 8 Stream findAny() method examples.

Optional<T> findAny()


toArray() Operation

The toArray() stream operation will return an array containing the elements of this stream. The toArray() operation has two different method signatures. See some working Java 8 Stream toArray() method examples.

Object[] toArray()
....
<A> A[] toArray(IntFunction<A[]> generator)


flatMap() Operation

The flatMap() stream operation will return 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. Take a look at the flatMap() method sample code.

<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)


peek() Operation

The peek() stream operation will return 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. Check out our peek() method sample code.

Stream<T> peek(Consumer<? super T> action)


Java References:

  1. Java 8 Stream Interface

Add a Comment

Your email address will not be published. Required fields are marked *