WEBVTT

00:00.860 --> 00:01.430
Hi everyone.

00:01.430 --> 00:02.720
Welcome back to this tutorial.

00:02.720 --> 00:09.920
In this tutorial I will talk some theory about Streams API and then we will implement a simple use case.

00:10.070 --> 00:13.610
Streams API got introduced in Java eight.

00:13.850 --> 00:20.480
The main purpose of streams API is to perform some kind of operation on a collection, meaning the collection

00:20.480 --> 00:25.400
could be a set list, linked list or map and et-cetera.

00:25.430 --> 00:31.730
You might be wondering why collection write any experienced Java developer out there knows the importance

00:31.730 --> 00:32.720
of collections.

00:32.750 --> 00:38.540
Collections help you group organize the data in our in your application.

00:38.750 --> 00:45.140
Basically any Java application that is out there, you heavily uses the collections.

00:46.400 --> 00:51.620
Even parallel operations are really easy to perform using streams API.

00:52.070 --> 00:53.840
That wasn't the case before.

00:54.060 --> 01:01.280
Forkjoin, which got introduced as part of Java seven, made it easier to perform parallel operation,

01:01.280 --> 01:04.670
but Streams API made it really simpler.

01:06.220 --> 01:12.040
API can also be used alongside with arrays or any kind of iOS.

01:13.370 --> 01:15.350
We talked about streams a bit.

01:15.380 --> 01:18.380
Now let's talk about what is a stream in general.

01:18.800 --> 01:25.640
A stream is a sequence of elements that are created from a collection, are arrays or any kind of IO.

01:25.700 --> 01:29.480
The next question that might be running in your mind is how do we create it?

01:29.480 --> 01:30.110
Right?

01:30.290 --> 01:31.730
Let's take a look at this example.

01:31.730 --> 01:34.640
We have a list of names which is of type list.

01:34.760 --> 01:36.410
We all know list is a collection.

01:36.440 --> 01:40.520
The way to create a stream out of it is by calling the stream method.

01:41.310 --> 01:44.250
This will create a stream of strings for you.

01:45.540 --> 01:51.960
As I mentioned before, streams operations can be performed sequentially or in parallel.

01:52.170 --> 01:53.640
How do we create a parallel stream?

01:53.640 --> 01:54.270
Right.

01:54.480 --> 02:01.410
Replace the stream with a parallel stream method that will help performing the stream operations in

02:01.410 --> 02:02.190
parallel.

02:02.220 --> 02:05.670
It is that simple to create a parallel stream.

02:05.970 --> 02:09.630
Now let's go ahead and code a simple example.

02:09.990 --> 02:11.610
I have the IntelliJ open.

02:11.640 --> 02:12.510
There you go.

02:12.660 --> 02:16.380
Now the first step is I'm going to create a package called streams.

02:18.670 --> 02:22.750
And then I'm going to create a class called Streams example.

02:27.500 --> 02:31.800
And I'm going to make this class executable by adding public static void main method.

02:32.370 --> 02:32.970
There you go.

02:33.000 --> 02:40.170
The first step is we'll code the use case where I want the student and their list of activities in a

02:40.170 --> 02:40.680
map.

02:40.710 --> 02:44.730
How do we implement that use case using streams API.

02:44.760 --> 02:46.200
So the use cases.

02:47.570 --> 02:48.260
The name.

02:50.900 --> 02:53.600
And their activities.

02:54.580 --> 02:55.110
No map.

02:55.900 --> 02:56.290
Okay.

02:57.430 --> 03:03.580
So we we know the student database class has a method called get all students, which is going to return

03:03.580 --> 03:06.160
you all the students in the collection.

03:06.550 --> 03:07.000
Here we go.

03:07.000 --> 03:08.110
We have six students.

03:08.320 --> 03:14.410
My use case is to have the student as a key and these list of activities as a value.

03:15.050 --> 03:15.930
I recorded.

03:16.230 --> 03:20.700
The first step is we are going to create the stream by calling the stream method.

03:21.180 --> 03:21.600
Good.

03:21.630 --> 03:27.420
The next step is we are going to use a method called collect.

03:28.790 --> 03:33.230
And there is another class called collectors.

03:35.870 --> 03:39.170
That is going to have a method called to map.

03:39.800 --> 03:40.370
There you go.

03:40.370 --> 03:43.880
We're going to use this method and if you take a look at it, it is using the.

03:45.330 --> 03:50.520
Go to the method and then check it is using the function as the first parameter and again function as

03:50.520 --> 03:51.810
a second parameter.

03:51.810 --> 03:54.210
We are going to make use of this.

03:55.400 --> 03:55.970
Method.

03:57.140 --> 03:59.750
So here we are going to provide.

03:59.960 --> 04:00.680
Student.

04:02.050 --> 04:04.120
We are going to use the method reference.

04:05.010 --> 04:05.820
It name.

04:07.700 --> 04:09.110
Let's import the student.

04:10.780 --> 04:15.490
As soon as I give the method reference syntax, it is going to give me the recommendation to use them

04:15.490 --> 04:17.560
name followed by that.

04:17.770 --> 04:18.730
What do we want?

04:19.330 --> 04:19.870
We want.

04:19.900 --> 04:20.980
Student Colon.

04:20.980 --> 04:21.580
Colon.

04:22.750 --> 04:23.980
IT activities.

04:25.620 --> 04:27.290
So this is returning a map.

04:27.300 --> 04:34.440
I'm going to create a map of string followed by the list of.

04:35.090 --> 04:38.690
Activities meaning list of string which has the activities.

04:39.260 --> 04:41.030
Let's give it a name for this one.

04:41.330 --> 04:47.180
Import this Java.util map, import this Java.util package.

04:47.180 --> 04:49.760
So this is like a student map.

04:50.890 --> 04:51.250
Going to.

04:51.760 --> 04:53.650
I'm going to print the.

04:55.310 --> 04:55.530
Button.

04:55.550 --> 04:55.820
Map.

04:58.440 --> 04:58.940
There you go.

04:58.950 --> 05:02.670
Now the next step, what I'm going to do, I'm going to run this example.

05:07.920 --> 05:12.540
So here it printed the student and the list of activities.

05:13.110 --> 05:17.880
Let's take a look at the example that we coded in one of our previous tutorial where let's take a look

05:17.880 --> 05:19.560
at this by function example.

05:19.590 --> 05:25.770
Here also, we have created a map with the student name as a key and the GPA as a value.

05:25.770 --> 05:29.370
But if you take a look at the code comparing this to the streams.

05:31.210 --> 05:36.370
For the number of lines of code and how descriptive this particular code is.

05:36.370 --> 05:37.660
It is more readable.

05:37.690 --> 05:39.780
That's one of the advantage with streams.

05:40.780 --> 05:47.830
You might be wondering, I said the same thing for lambdas, but now I'm saying that streams API is

05:47.830 --> 05:49.150
better than lambdas.

05:49.180 --> 05:50.350
The answer is no.

05:50.350 --> 05:52.600
That was not my intention at all.

05:53.110 --> 05:56.470
Streams API and lambdas go hand in hand.

05:56.500 --> 06:00.670
Now let's enhance this use case by adding more conditions to it.

06:00.700 --> 06:06.910
Now I want just the students who are in the grade level greater than or equal to three.

06:06.940 --> 06:09.800
In that case, how do I implement that?

06:09.820 --> 06:10.420
Right.

06:10.420 --> 06:17.580
There is a method called filter, which is part of the streams API, which is going to use lambdas.

06:17.590 --> 06:21.710
So if you go and take a look at the filter implementation, it accepts a predicate.

06:21.730 --> 06:25.390
So we are going to implement the predicate using the lambda.

06:25.930 --> 06:33.880
So here it is going to give you a student list stream of students because we have created a.

06:35.160 --> 06:35.550
Dream.

06:35.550 --> 06:40.300
All of this list of students, basically, that will help you create a stream of students.

06:40.320 --> 06:42.690
Now it is going to accept a student.

06:43.660 --> 06:48.700
Our filter is student dot get grade level.

06:49.430 --> 06:50.660
Greater than or equal to three.

06:51.080 --> 06:56.390
So right now in here, we have six students in the result.

06:56.870 --> 06:57.410
Okay.

06:57.410 --> 07:00.630
Now the use cases, we have applied this filter.

07:00.650 --> 07:06.800
Let's see how many number of students are going to be printed as a result of that filter.

07:09.120 --> 07:14.790
As you see right now, we just have three students, not not three students, four students who are

07:14.790 --> 07:16.500
matching this filter criteria.

07:17.040 --> 07:22.230
I'm going to enhance this use case by creating a predicate.

07:24.820 --> 07:25.690
Of shouldn't.

07:27.860 --> 07:29.900
I'm going to name this one as predicate student.

07:30.730 --> 07:31.690
And then.

07:33.750 --> 07:35.610
Goodness, Right.

07:35.910 --> 07:39.870
So here we have created a predicate.

07:39.900 --> 07:45.570
Now we can parse this predicate in this API filter.

07:46.530 --> 07:49.290
But understand is going to give me the same result.

07:49.560 --> 07:53.370
Now the next step is I want to apply another level of filtering.

07:55.390 --> 07:59.650
So I'm gonna name this one as student predicate.

08:00.850 --> 08:04.940
Yet GPA is greater than or equal to 3.9.

08:04.960 --> 08:10.490
That's a condition I want to apply for this existing filter so we already have a filter.

08:10.510 --> 08:15.370
Let's apply one more filter that's going to be student GPA predicate.

08:15.850 --> 08:20.200
Now let's go ahead and check what is the result that it is going to print.

08:24.510 --> 08:25.590
Running this.

08:26.340 --> 08:26.850
Here we go.

08:27.120 --> 08:29.250
It printed three students.

08:29.250 --> 08:31.230
Let's go and check the database class.

08:32.630 --> 08:35.240
So here we have.

08:36.390 --> 08:37.670
One, two, three, four.

08:37.680 --> 08:39.160
How many students that got printed?

08:39.180 --> 08:41.760
We have three students who got printed.

08:42.030 --> 08:49.230
Sophia wasn't there because she is not having the level greater than or equal to 3.9.

08:50.940 --> 08:53.700
If I comment this code, it is going to.

08:54.850 --> 08:56.800
You mean for students?

08:56.830 --> 09:02.650
If I enable this filter, it is giving me three students and that is what is expected.

09:03.010 --> 09:10.930
So it is that simple to perform these kind of manipulations in streams API.

09:11.200 --> 09:16.210
Basically, this is what I have observed from my experience of teaching first time.

09:16.210 --> 09:20.850
When you look at something new, either it is a syntax or a new way of programming.

09:20.860 --> 09:27.160
It might not be clear at that moment, but the next time when you look at it, it is it will clear.

09:27.160 --> 09:31.570
And then when you actually work on it, it will be more clear.

09:31.750 --> 09:36.820
Let's say I want to create a parallel stream instead of a stream is very simple.

09:36.880 --> 09:43.420
Now the filtering operations and all these collect operations are going to be performed in parallel

09:43.420 --> 09:44.080
manner.

09:44.540 --> 09:45.130
Run it.

09:45.670 --> 09:52.390
The result is going to be the same, but under the hood it is running all the operations in parallel.

09:53.760 --> 09:55.560
Let's not worry about parallel streams.

09:55.560 --> 09:55.890
Now.

09:55.890 --> 10:00.990
I have a separate section planned in the future tutorials of this course that will cover more details

10:00.990 --> 10:02.050
on this topic.

10:02.070 --> 10:08.700
One more thing I would like to highlight here is that streams API manipulate the collections in a declarative

10:08.700 --> 10:09.120
way.

10:09.150 --> 10:15.840
What does it mean is that instead of writing the code on how to implement the logic, just ask the desired

10:15.840 --> 10:19.980
result using the functions that are already part of the streams API.

10:20.010 --> 10:23.460
So here we have passed the filter predicate, right?

10:23.460 --> 10:25.110
The student predicate to this filter method.

10:25.140 --> 10:31.320
We don't care how it is going to implement or use that predicate to give you the desired result.

10:32.010 --> 10:38.100
A very good analogy for this style of programming, which is declarative programming is SQL.

10:38.250 --> 10:40.680
We run queries against the database tables.

10:40.680 --> 10:45.810
We don't care the computation that it performs behind the scenes to give you the desired result.

10:45.930 --> 10:47.970
We just focus on getting the result.

10:48.860 --> 10:52.160
Let's discuss more on the streams in the following sections.

10:52.190 --> 10:53.460
With this, we came to the end of.
