stream_api_1724822016
stream_api_1724822016
STREAM API
Prepared By
task. Whether you’re filtering, transforming, or analyzing data, the process often requires
looping over collections and manually applying operations. This is where Java's Stream API
comes in, making these operations much simpler and more readable.
A Stream is a not a data structure instead it takes input from the collections or arrays, Stream
don’t change original data structure; it produces the result as per the methods from source.
Stream is lazy and evaluates code only when required. Stream has two types of operations
Intermediate operations
Terminal operations
An intermediate operation is a lazy operation, which means that these operations are not
executed immediately. Instead, they are executed only when the execution reaches a terminal
operation. This lazy nature allows for optimization and efficiency when processing streams.
Let’s look at some of the most popular intermediate operations used in streams:
1. Map:
The Map operation is used to returns a stream consisting of the results of applying the given
2. Filter:
The Filter operation is used to return a stream of elements which satisfies the given predicate
3. Limit:
The limit operation restricts the number of elements in the stream to a specified size. This is
particularly useful when you need to process only a portion of the stream.
4. Distinct:
The distinct operation removes duplicates from the stream, ensuring that each element
appears only once in the resulting stream.
Terminal Operations
A Terminal operation is Eager operation that is to say, they start the execution of all previous
lazy operations presented in the stream. Terminal operation either return concrete types or
produce a result list. i.e., a reduce operation is returns single value by reducing the results into
a single sum. A collect operation will create a new list of results, here are the most popular
Collect is used to return the list, map or set of results of intermediate operations performed on
stream
2. ForEach :
ForEach is used to iterate through every element of stream after completing the
3. Reduce :
Reduce is used to return the single value by cumulating the elements of stream after execution
of intermediate operation
Problem : Java program to print all the elements of list by multiplying with two ?
Traditional Solution
Note: here by using method reference syntax i printed the data using system.out::println
Problem : Java program to print strings of an array which started with a specific keyword,
print all strings in uppercase
Traditional Solution
Problem : Java program to print sum of even numbers from the list
Traditional Solution
Stream API Solution
Note: here we can use sum method to calculate the sum of all elements, Instead of sum we can use reduce method
also to calculate sum as .reduce(0,(i,j)->i+j);
Traditional Solution
Practice Problems
Java program to calculate the sum of all even, odd numbers in a list using
streams
Java program to count the frequency of each word (ignoring case) and return a
map with words as keys and their frequencies as values
Java program to find the longest string in the list. If there are multiple strings
with the same maximum length,
Java program to print the total salary of the employees grouped by department
using the Stream API