WEBVTT

00:00.880 --> 00:01.440
Hi everyone!

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

00:02.720 --> 00:08.720
In this tutorial I will explain you about the stateful and stateless function that are part of the streams

00:08.720 --> 00:09.240
API.

00:10.200 --> 00:13.600
The first question is does streams have an internal state?

00:13.960 --> 00:15.160
The answer is yes.

00:16.040 --> 00:19.400
Does all the streams functions maintain an internal state?

00:19.760 --> 00:21.400
The answer is no.

00:21.440 --> 00:25.280
We have covered a lot of different stream functions that are part of the streams API.

00:25.640 --> 00:32.520
As part of the previous tutorials, let's take a look at the example and find out where exactly the

00:32.520 --> 00:37.720
stream state context resides as part of the stream pipeline.

00:37.760 --> 00:42.880
So this example is going to convert a list of student to list of string.

00:42.880 --> 00:47.080
We have coded the same example in one of the prior tutorials.

00:47.080 --> 00:52.720
If you take a look at it, we are starting the stream here and then we are performing a map call.

00:53.120 --> 01:00.440
This is going to convert a list of student to a list of names, basically stream of students to a stream

01:00.440 --> 01:01.240
of string.

01:01.440 --> 01:06.070
And then we are performing an uppercase operation on that stream of string.

01:06.350 --> 01:06.950
And there we are.

01:07.030 --> 01:09.870
And then we are performing the select operation.

01:10.150 --> 01:12.790
This whole thing is called a stream pipeline.

01:13.230 --> 01:16.310
And these two are intermediate operations.

01:16.550 --> 01:19.590
And then collect is the terminal operation.

01:20.190 --> 01:25.950
So the stream state resides in the context of the intermediate operations.

01:25.950 --> 01:31.990
Intermediate operations is where the streams state is going to reside.

01:33.350 --> 01:39.230
I would like to highlight that not all the functions maintain the stream internal state.

01:39.270 --> 01:40.350
The explain here.

01:40.870 --> 01:44.190
The example here doesn't maintain the internal state.

01:44.190 --> 01:48.390
I will explain why in a moment.

01:48.470 --> 01:51.950
The intermediate operations are categorized into two.

01:51.990 --> 01:56.270
One is a stateful functions and another one is a stateless functions.

01:56.270 --> 02:04.020
Whatever that are mentioned here are the stateful functions distinct sorted skip and limit.

02:04.020 --> 02:06.580
These are stateful functions.

02:06.860 --> 02:07.420
Whatever.

02:07.700 --> 02:12.940
The other functions that are not listed here are called stateless functions.

02:13.220 --> 02:17.420
The examples are map, filter, and etc..

02:18.220 --> 02:23.940
I'm going to show you an example of stateful functions first.

02:23.980 --> 02:28.060
We have coded this example as part of the distinct and sorted tutorial.

02:28.700 --> 02:33.660
This example is going to convert a list of student to list of string.

02:34.620 --> 02:39.820
We all know that stream function call here is going to pass the elements one by one.

02:40.260 --> 02:45.340
Before I get into the details of it, I would like to explain this example again and then we'll discuss

02:45.340 --> 02:47.540
more about the stateful functions.

02:47.660 --> 02:50.540
Here is where we are calling the stream method.

02:50.700 --> 02:55.860
And then the stream method is going to pass the students one by one.

02:56.060 --> 03:00.660
So the map here is performing student activities.

03:00.660 --> 03:04.260
So activities is of type list if you remember from the example.

03:04.360 --> 03:08.880
And then this is going to pass a list of students to the flatMap.

03:09.080 --> 03:10.880
And then we are flattening the stream.

03:10.960 --> 03:15.880
This is going to pass a stream of string to the distinct operation.

03:16.080 --> 03:21.960
Here is where we are performing the distinct operation which is going to remove the duplicates.

03:21.960 --> 03:28.000
And then we are performing the sorted operation to sort the list of activities in the alphabetical order.

03:28.160 --> 03:31.200
And then we are performing the collect operation.

03:31.560 --> 03:39.320
So basically this is going to give you a list of unique student activities as a result.

03:39.360 --> 03:39.720
Right.

03:39.880 --> 03:49.120
So in this pipeline until the distinct function, the whatever functions that we are calling are stateless,

03:49.440 --> 03:56.640
distinct function is where the stateful context comes into picture, because that's when the streams

03:56.680 --> 04:03.600
API need to preserve the state of the previous items, whatever the previous stream elements that it

04:03.600 --> 04:04.160
processed.

04:04.400 --> 04:05.030
Because Gus.

04:05.430 --> 04:08.110
This drink is going to remove the duplicates, right?

04:08.190 --> 04:13.390
So every time an element is passed to this distinct function, it needs to know whether that element,

04:13.430 --> 04:19.830
whatever that is being passed as a current element, is already available in the previously processed

04:19.830 --> 04:25.150
elements, so that it can consider that one as a duplicate and remove it as a result.

04:25.350 --> 04:28.470
And then here we are performing the sorted operation.

04:28.470 --> 04:33.670
Started operation by default is going to sort the result in alphabetical order.

04:33.950 --> 04:40.550
So again it needs to know the previous elements that it processed because so that it can insert or it

04:40.550 --> 04:45.270
can modify the order based on the current element that is being passed.

04:46.430 --> 04:53.590
So distinct and sorted is where it needs to know the state of the previously processed elements.

04:53.710 --> 04:58.990
This is where the stream API state context comes into picture.

05:02.870 --> 05:05.710
Now let's discuss about stateless functions.

05:06.190 --> 05:12.620
The stream functions that does not maintain any state are called stateless stream functions.

05:12.820 --> 05:15.660
Let's dig in further and understand what does it mean?

05:15.900 --> 05:21.820
So in this example again we are going to convert a list of students to a list of string.

05:21.860 --> 05:25.300
So here if you take a look at it first college names dot stream.

05:25.300 --> 05:26.380
We are creating a stream.

05:26.380 --> 05:30.220
And then the stream elements are going to be parsed one by one.

05:30.380 --> 05:37.140
So here we have the map call which is just going to transfer a stream of student to a stream of string.

05:37.820 --> 05:39.140
The stateless operation.

05:39.260 --> 05:43.100
It doesn't need to know what are the elements that it processed prior.

05:43.140 --> 05:45.140
The next thing is again we are converting.

05:45.340 --> 05:52.900
We are calling the map method, and then we are converting a list of stream of strings and then applying

05:53.180 --> 05:55.260
an uppercase operation on top of it.

05:55.300 --> 05:57.460
Basically the type is going to be the same.

05:57.580 --> 06:00.460
It is just that we are performing an uppercase operation to.

06:00.460 --> 06:02.260
It is again stateless.

06:02.660 --> 06:04.820
So here it doesn't even apply.

06:05.260 --> 06:11.520
So we don't even have to consider this Because the state context comes into picture only for the intermediate

06:11.520 --> 06:12.320
operation.

06:12.440 --> 06:16.960
In this example, the intermediate operations are just map.

06:17.520 --> 06:19.520
I would like to explain more about it.

06:19.520 --> 06:24.520
So we will go to the example that we coded previously and then discuss more on it.

06:26.640 --> 06:30.760
So I'm going to open a streams flatMap example here.

06:32.120 --> 06:33.840
Let me put this in presentation mode.

06:38.920 --> 06:39.640
So in here.

06:40.080 --> 06:42.720
So this is the example which we discussed as part of the presentation.

06:42.720 --> 06:47.400
But I would like to give you some more code comments and then make it clear.

06:47.760 --> 06:53.000
So here we are performing didn't get activities.

06:53.000 --> 06:59.880
This is of type list is going to convert a stream of student to stream of list of string.

07:00.160 --> 07:02.800
So this is a stateless operation.

07:03.880 --> 07:06.640
And then here we are flattening the stream.

07:06.640 --> 07:11.190
Basically we are converting a stream of list of string to stream of string.

07:11.190 --> 07:13.110
It is again stateless.

07:14.710 --> 07:18.550
So here is where we are going to perform the distinct operation.

07:18.550 --> 07:23.230
Basically distinct operation is going to remove the duplicates right.

07:23.630 --> 07:25.790
So this is a stateful operation.

07:25.790 --> 07:28.950
It needs to know about the previously processed elements.

07:29.910 --> 07:35.070
So that if it is a duplicate it can remove that element from the result.

07:35.070 --> 07:42.590
And next thing is a sorted sorted here is going to sort the result in the ascending order.

07:42.790 --> 07:48.550
So it needs to know the state of the previously processed elements so that it can sort accordingly as

07:48.550 --> 07:50.910
per the current element.

07:50.910 --> 07:55.470
And the next one is a terminal operation which is going to perform the collect.

07:56.030 --> 08:00.510
And then it is going to give you the list of unique activities as a result.

08:01.110 --> 08:05.830
So this is all about the stateful and stateless functions.

08:06.630 --> 08:08.590
With this we came to the end of this tutorial.

08:08.710 --> 08:09.670
Thank you for watching.
