top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Java Stream


What is Java Stream?


Java Stream is a new feature which is available from Java 8. By using stream we can perform various aggregate operations on the data returned from the collections classes by drastically reducing the complexity of the code.

It allows us to perform operations such as filter, map, sort and reduce in a collection of data. The greatest advantage of using stream is avoiding the multiple lines of "for loop and achieving the data manipulation with a single line of code, which makes our program optimized and easy to understand.



How Java Streams work?


To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(Predicate)), and a terminal operation (which produces a result or side-effect, such as count() or forEach(Consumer)). Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.


The Working of stream can be explained in three stages:


  1. Create a stream

  2. Perform intermediate operations on the initial stream to transform it to another stream and so on further intermediate operations.

  3. Perform terminal operation on the final stream to get the result.


  1. Creating a stream


We can create a stream from different sources of data. Here are the multiple ways of creating a stream


  • From a Collection


Streams can be created from an ArrayList or set using the stream() method:









  • From an Array


Streams can be created from an Array using the Array.stream() method:





  • Stream.of() method:





2. Intermediate Operations:


Intermediate operations are the types of operations in which multiple methods are chained in a row .


An important characteristic of intermediate operations is laziness. When executing this code snippet, nothing is printed to the console. That is because intermediate operations will only be executed when a terminal operation is present.


Some of the important intermediate operations are


  • filter()

  • map()

  • sorted()

  • reduce()



filter()

The filter method is used to select elements as per the predicate passed as an argument. Predicate means whether the given argument's return value is true.








Output:  2

In the above example the .filter() filters based upon the condition specified inside the filter method.


Lambda expressions introduce the new arrow operator -> into Java. It divides the lambda expressions into two parts:






The left side "s" specifies the parameters required by the expression and the right side is the lambda body which specifies the actions of the lambda expression.


Here the "s" denotes the parameter to which we need pass the data and then the right side of the lambda expression which specifies the condition or action we will be applying to filter the data to work on. So in this line of code we are filtering the data with the s.startswith("J") method which will filter the names that starts with the letter "J" from the list and stores it as a new stream. The terminal operator count() method gives the count of names that starts with the letter "J".


The below code shows the alternate way to perform the same operation using the for loop. But with the java stream the code is optimized to just a single line.












map()


The map method is used to perform an operation over the collection list. Lets see with the below example where we are trying to print the name that ends with the letter 'g' in the uppercase.






Output: 
PROGRAMMING
TESTING

The filter method checks for the condition and extract the names which ends with the letter 'g' and that stream is passed to the map method where this new stream with extracted data is used to print the names in the upper case.


sorted()


This method sort the elements in the stream.





Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The list of numbers are passed into the stream to sort using the sorted method and the results are displayed with the help of the terminal operator collect.



3. Terminal Operations


Terminal Operations are the type of Operations that return the result. These Operations are not processed further just return a final result value. Here are some of the terminal operations below


  • collect()

The collect method is used to return the result of the intermediate operations performed on the stream.


  • forEach() The forEach method is used to iterate through every element of the stream.







Apart from this we have a vast variety of terminal operations which should be always followed by an intermediate operation.


Stream and collections might sound similar but there are major differences between. Lets list out the differences between them


STREAM

COLLECTIONS

It doesn’t store data, it operates on the source data structure i.e collection

It stores/holds all the data that the data structure currently has in a particular data structure like Set, List or Map,

They use functional interfaces like lambda which makes it a good fit for programming language.

Don’t use functional interfaces.

Java Streams are consumable i.e; to traverse the stream, it needs to be created every time.

Non-consumable i.e; can be traversable multiple times without creating it again.

Java streams support both sequential and parallel processing.

Supports parallel processing and parallel processing can be very helpful in achieving high performance.

All the Java stream API interfaces and classes are in java.util.stream package.

Specific classes for primitive types such as IntStream, LongStream, and DoubleStream are used in collections since primitive data types such as int, long in the collections using auto-boxing and these operations could take a lot of time.

Streams are not modifiable i.e one can’t add or remove elements from streams.

These are modifiable i.e one can easily add to or remove elements from collections.

Streams are iterated internally by just mentioning the operations.

Collections are iterated externally using loops

Conclusion:

Java stream is a powerful and much optimized method to work with the collections and a sequence of data. With few simple operations we can map, filter and aggregate data efficiently.


In this blog we explored about the java stream in the next blog we will learn about how to use these advanced and optimized Java stream API in selenium to perform web table validations and sorting.

42 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page