Stream API introduction – java 8

/, Java 8/Stream API introduction – java 8

What is Stream in java 8?

A Stream is a sequence of elements supporting sequential and parallel aggregate operations. A stream is to perform operations on collection of elements, which might be an array, a collection, a generator function, an I/O channel, etc. Streams provide a declarative approach to Collections.

To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source, intermediate operations and terminal operations.

1. Source – which might be an array, a collection, a generator function, an I/O channel, etc
2. Intermediate operations– which transform a stream into another stream.
3. Terminal operation– which produces a result or side-effect.

Characteristics of Java Streams :

1. Streams are Functional in nature. They work perfectly with lambdas.
2. No storage – Streams don’t store any elements.
3. Consumable- The elements of a stream are only visited once during the life of a stream. They are not reusable.
4. Streams are immutable.
5. Streams are sequential or parallelized.
6. Laziness-seeking – Intermediate operations are always lazy.

Intermediate operations :

Operation Name and Description
Returns a stream consisting of the elements of this stream that match the given predicate.
Returns a stream consisting of the results of applying the given function to the elements of this stream.
Returns an IntStream consisting of the results of applying the given function to the elements of this stream.
Returns a LongStream consisting of the results of applying the given function to the elements of this stream.
Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.
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. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
Returns an IntStream 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 null an empty stream is used, instead.)
Returns an LongStream 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 null an empty stream is used, instead.)
Returns an DoubleStream 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 placed been into this stream. (If a mapped stream is null an empty stream is used, instead.)
Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream.
Returns a stream consisting of the elements of this stream, sorted according to natural order. If the elements of this stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed.
Returns a stream consisting of the elements of this stream, sorted according to the provided 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.
Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.

Terminal operations :

Operation Name and Description
Performs an action for each element of this stream.
Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
Returns an array containing the elements of this stream.
Returns an array containing the elements of this stream, using the provided generator function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.
Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions.
Performs a mutable reduction operation on the elements of this stream. A mutable reduction is one in which the reduced value is a mutable result container, such as an ArrayList, and elements are incorporated by updating the state of the result rather than by replacing the result.
Performs a mutable reduction operation on the elements of this stream using a Collector. A Collector encapsulates the functions used as arguments to collect(Supplier, BiConsumer, BiConsumer), allowing for reuse of collection strategies and composition of collect operations such as multiple-level grouping or partitioning.

Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.This is a terminal operation.

Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then false is returned and the predicate is not evaluated.
Returns the count of elements in this stream.
Returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated.
Returns whether no elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated.
Returns 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, then any element may be returned.
Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.

A simple example without streams : printing even numbers from collection

A simple example with streams : printing even numbers from collection

Leave a Reply