What Is Java Stream?
- Java stream introduced in Java 8
- Stream is nothing but a collection of strings
- It is a sequence of objects that supports various methods which can be pipelined to produce the desired output
- We will understand more with the help of a diagram about the java stream
Some Key Features of Java Stream
- It is not a data structure but it takes input from collections, arrays, etc.
- It does not change the original data structures but it provides the output as per the pipelined methods.
Here are some different operations related to the streams. There are many methods/operations in the Java streams but we will discuss Some methods in this blog that we commonly use in our day-to-day life.
1)Map
It is an intermediate operation of the java stream. It is used to return a stream consisting of the results of applying the given function to the elements of this stream.
Let’s Have a Quick Example of the Map Operations
The output of the above program:
2)Filter Operation
Filter operation is also an intermediate operation and it is used to select elements as per the Predicate passed as an argument.
Let’s have a Quick example of the filter operation:
The output of the above program
3)Sorted
Sorted is also an intermediate operation. And it is used to sort a stream.
Let’s have a Quick example of the Sorted operation:
The output of the above program:
Recommended to Read – How to Import Excel Data to SQLite DB Using Java?
4)Collect
Colette operation is a terminal operation and It use to return the result of the intermediate operations performed on the stream.
Let’s have a Quick example of the Collect operation
The output of the above program:
5)Foreach
It is also a terminal operation and It uses to iterate through every element of the string.
Let’s have a Quick example of the Foreach operation:
Recommended to Read:- The Execution Time Of Multiple Methods In Selenium Java?
6)Reduce
It is also a terminal operation. And its uses to reduce the elements of a stream to a single value. and it takes a binary operation as a parameter.
Let’s have a Quick example of the reduced operation
The output of the above program
Overall summarize these are some methods that we have seen in this blog with examples. I hope it will help you.
If you are interested in even more software testing-related articles and information from us here at Devstringx, then we have a lot to choose from for you.