Hi everyone.
Welcome back to this tutorial.
In this tutorial I will talk some theory about Streams API and then we will implement a simple use case.
Streams API got introduced in Java eight.
The main purpose of streams API is to perform some kind of operation on a collection, meaning the collection
could be a set list, linked list or map and et-cetera.
You might be wondering why collection write any experienced Java developer out there knows the importance
of collections.
Collections help you group organize the data in our in your application.
Basically any Java application that is out there, you heavily uses the collections.
Even parallel operations are really easy to perform using streams API.
That wasn't the case before.
Forkjoin, which got introduced as part of Java seven, made it easier to perform parallel operation,
but Streams API made it really simpler.
API can also be used alongside with arrays or any kind of iOS.
We talked about streams a bit.
Now let's talk about what is a stream in general.
A stream is a sequence of elements that are created from a collection, are arrays or any kind of IO.
The next question that might be running in your mind is how do we create it?
Right?
Let's take a look at this example.
We have a list of names which is of type list.
We all know list is a collection.
The way to create a stream out of it is by calling the stream method.
This will create a stream of strings for you.
As I mentioned before, streams operations can be performed sequentially or in parallel.
How do we create a parallel stream?
Right.
Replace the stream with a parallel stream method that will help performing the stream operations in
parallel.
It is that simple to create a parallel stream.
Now let's go ahead and code a simple example.
I have the IntelliJ open.
There you go.
Now the first step is I'm going to create a package called streams.
And then I'm going to create a class called Streams example.
And I'm going to make this class executable by adding public static void main method.
There you go.
The first step is we'll code the use case where I want the student and their list of activities in a
map.
How do we implement that use case using streams API.
So the use cases.
The name.
And their activities.
No map.
Okay.
So we we know the student database class has a method called get all students, which is going to return
you all the students in the collection.
Here we go.
We have six students.
My use case is to have the student as a key and these list of activities as a value.
I recorded.
The first step is we are going to create the stream by calling the stream method.
Good.
The next step is we are going to use a method called collect.
And there is another class called collectors.
That is going to have a method called to map.
There you go.
We're going to use this method and if you take a look at it, it is using the.
Go to the method and then check it is using the function as the first parameter and again function as
a second parameter.
We are going to make use of this.
Method.
So here we are going to provide.
Student.
We are going to use the method reference.
It name.
Let's import the student.
As soon as I give the method reference syntax, it is going to give me the recommendation to use them
name followed by that.
What do we want?
We want.
Student Colon.
Colon.
IT activities.
So this is returning a map.
I'm going to create a map of string followed by the list of.
Activities meaning list of string which has the activities.
Let's give it a name for this one.
Import this Java.util map, import this Java.util package.
So this is like a student map.
Going to.
I'm going to print the.
Button.
Map.
There you go.
Now the next step, what I'm going to do, I'm going to run this example.
So here it printed the student and the list of activities.
Let's take a look at the example that we coded in one of our previous tutorial where let's take a look
at this by function example.
Here also, we have created a map with the student name as a key and the GPA as a value.
But if you take a look at the code comparing this to the streams.
For the number of lines of code and how descriptive this particular code is.
It is more readable.
That's one of the advantage with streams.
You might be wondering, I said the same thing for lambdas, but now I'm saying that streams API is
better than lambdas.
The answer is no.
That was not my intention at all.
Streams API and lambdas go hand in hand.
Now let's enhance this use case by adding more conditions to it.
Now I want just the students who are in the grade level greater than or equal to three.
In that case, how do I implement that?
Right.
There is a method called filter, which is part of the streams API, which is going to use lambdas.
So if you go and take a look at the filter implementation, it accepts a predicate.
So we are going to implement the predicate using the lambda.
So here it is going to give you a student list stream of students because we have created a.
Dream.
All of this list of students, basically, that will help you create a stream of students.
Now it is going to accept a student.
Our filter is student dot get grade level.
Greater than or equal to three.
So right now in here, we have six students in the result.
Okay.
Now the use cases, we have applied this filter.
Let's see how many number of students are going to be printed as a result of that filter.
As you see right now, we just have three students, not not three students, four students who are
matching this filter criteria.
I'm going to enhance this use case by creating a predicate.
Of shouldn't.
I'm going to name this one as predicate student.
And then.
Goodness, Right.
So here we have created a predicate.
Now we can parse this predicate in this API filter.
But understand is going to give me the same result.
Now the next step is I want to apply another level of filtering.
So I'm gonna name this one as student predicate.
Yet GPA is greater than or equal to 3.9.
That's a condition I want to apply for this existing filter so we already have a filter.
Let's apply one more filter that's going to be student GPA predicate.
Now let's go ahead and check what is the result that it is going to print.
Running this.
Here we go.
It printed three students.
Let's go and check the database class.
So here we have.
One, two, three, four.
How many students that got printed?
We have three students who got printed.
Sophia wasn't there because she is not having the level greater than or equal to 3.9.
If I comment this code, it is going to.
You mean for students?
If I enable this filter, it is giving me three students and that is what is expected.
So it is that simple to perform these kind of manipulations in streams API.
Basically, this is what I have observed from my experience of teaching first time.
When you look at something new, either it is a syntax or a new way of programming.
It might not be clear at that moment, but the next time when you look at it, it is it will clear.
And then when you actually work on it, it will be more clear.
Let's say I want to create a parallel stream instead of a stream is very simple.
Now the filtering operations and all these collect operations are going to be performed in parallel
manner.
Run it.
The result is going to be the same, but under the hood it is running all the operations in parallel.
Let's not worry about parallel streams.
Now.
I have a separate section planned in the future tutorials of this course that will cover more details
on this topic.
One more thing I would like to highlight here is that streams API manipulate the collections in a declarative
way.
What does it mean is that instead of writing the code on how to implement the logic, just ask the desired
result using the functions that are already part of the streams API.
So here we have passed the filter predicate, right?
The student predicate to this filter method.
We don't care how it is going to implement or use that predicate to give you the desired result.
A very good analogy for this style of programming, which is declarative programming is SQL.
We run queries against the database tables.
We don't care the computation that it performs behind the scenes to give you the desired result.
We just focus on getting the result.
Let's discuss more on the streams in the following sections.
With this, we came to the end of.