6- Stream in Java 8

 

   Java 8 Stream

Java provides a new additional package in Java 8 called java.util.stream. 

This package consists of classes, interfaces and enum to allows functional-style operations on the elements. 

Java streams enable functional-style operations on streams of elements. 

A stream is an abstraction of a non-mutable collection of functions applied in some order to the data. A stream is not a collection where you can store elements.

OR

Java streams represent a pipeline through which data will flow and the functions to operate on the data. 

A pipeline in this instance consists of a stream source, followed by zero or more intermediate operations, and a terminal operation.



When to Use Java Streams?

The most important pattern for using with the streams:

--> Raise a collection to a stream.

--> Ride the stream: filter values, transform values, limit the output.

--> Compose small individual operations.

--> Collect the result back into a concrete collection.


Why do we need Streams?

--> Functional Programming

--> Code Reduced by significant amount

--> Bulk Operations managed easily


Common Operations in Java Streams

Here are some common operations in Java streams:

Filter:

 Returns a new stream that contains some of the elements of the original.

 In the imperative code we would employ the conditional logic to specify what should happen if an element satisfies the condition. 

Map:

 Transforms the stream elements into something else, it accepts a function to apply to each and every element of the stream and returns a stream of the values the parameter function produced.

 This is the bread and butter of the Java streaming API. Map allows you to perform a computation on the data inside a stream.

Reduce:

 (also sometimes called a fold) Performs a reduction of the stream to a single element. If you want to sum all the integer values in the stream, you want to use the reduce function. If you want to find the maximum in the stream, reduce is your friend.

Collect: 

This is the way to get out of the streams world and obtain a concrete collection of values, like a list.


Coding Part

List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
       
List result = numbers.stream()
        .filter(e -> (e % 2) == 0) //Only even values move forward i.e(2,4)
        .map(e -> e * 2).toList(); //Generating new values(4,8) and mapping to original(2,4)
       
        System.out.println(result);

CONSOLE

[4, 8]

---------------------------

Features of Stream

 Stream provides following features:

--> Stream does not store elements.

It simply conveys elements from a source such as a data structure, an array, or an I/O channel, through a pipeline of computational operations.

--> Stream is functional in nature.

Operations performed on a stream does not modify it's source. For example, filtering a Stream obtained from a collection produces a new Stream without the filtered elements, rather than removing elements from the source collection.

--> Stream is lazy and evaluates code only when required.

--> The elements of a stream are only visited once during the life of a stream.

Like an Iterator, a new stream must be generated to revisit the same elements of the source.

Comments

Popular posts from this blog

INDEX OF JAVA 8 and MICROSERVICES BLOG