Hi everyone!
Welcome back to this tutorial.
In this tutorial I will explain you about the stateful and stateless function that are part of the streams
API.
The first question is does streams have an internal state?
The answer is yes.
Does all the streams functions maintain an internal state?
The answer is no.
We have covered a lot of different stream functions that are part of the streams API.
As part of the previous tutorials, let's take a look at the example and find out where exactly the
stream state context resides as part of the stream pipeline.
So this example is going to convert a list of student to list of string.
We have coded the same example in one of the prior tutorials.
If you take a look at it, we are starting the stream here and then we are performing a map call.
This is going to convert a list of student to a list of names, basically stream of students to a stream
of string.
And then we are performing an uppercase operation on that stream of string.
And there we are.
And then we are performing the select operation.
This whole thing is called a stream pipeline.
And these two are intermediate operations.
And then collect is the terminal operation.
So the stream state resides in the context of the intermediate operations.
Intermediate operations is where the streams state is going to reside.
I would like to highlight that not all the functions maintain the stream internal state.
The explain here.
The example here doesn't maintain the internal state.
I will explain why in a moment.
The intermediate operations are categorized into two.
One is a stateful functions and another one is a stateless functions.
Whatever that are mentioned here are the stateful functions distinct sorted skip and limit.
These are stateful functions.
Whatever.
The other functions that are not listed here are called stateless functions.
The examples are map, filter, and etc..
I'm going to show you an example of stateful functions first.
We have coded this example as part of the distinct and sorted tutorial.
This example is going to convert a list of student to list of string.
We all know that stream function call here is going to pass the elements one by one.
Before I get into the details of it, I would like to explain this example again and then we'll discuss
more about the stateful functions.
Here is where we are calling the stream method.
And then the stream method is going to pass the students one by one.
So the map here is performing student activities.
So activities is of type list if you remember from the example.
And then this is going to pass a list of students to the flatMap.
And then we are flattening the stream.
This is going to pass a stream of string to the distinct operation.
Here is where we are performing the distinct operation which is going to remove the duplicates.
And then we are performing the sorted operation to sort the list of activities in the alphabetical order.
And then we are performing the collect operation.
So basically this is going to give you a list of unique student activities as a result.
Right.
So in this pipeline until the distinct function, the whatever functions that we are calling are stateless,
distinct function is where the stateful context comes into picture, because that's when the streams
API need to preserve the state of the previous items, whatever the previous stream elements that it
processed.
Because Gus.
This drink is going to remove the duplicates, right?
So every time an element is passed to this distinct function, it needs to know whether that element,
whatever that is being passed as a current element, is already available in the previously processed
elements, so that it can consider that one as a duplicate and remove it as a result.
And then here we are performing the sorted operation.
Started operation by default is going to sort the result in alphabetical order.
So again it needs to know the previous elements that it processed because so that it can insert or it
can modify the order based on the current element that is being passed.
So distinct and sorted is where it needs to know the state of the previously processed elements.
This is where the stream API state context comes into picture.
Now let's discuss about stateless functions.
The stream functions that does not maintain any state are called stateless stream functions.
Let's dig in further and understand what does it mean?
So in this example again we are going to convert a list of students to a list of string.
So here if you take a look at it first college names dot stream.
We are creating a stream.
And then the stream elements are going to be parsed one by one.
So here we have the map call which is just going to transfer a stream of student to a stream of string.
The stateless operation.
It doesn't need to know what are the elements that it processed prior.
The next thing is again we are converting.
We are calling the map method, and then we are converting a list of stream of strings and then applying
an uppercase operation on top of it.
Basically the type is going to be the same.
It is just that we are performing an uppercase operation to.
It is again stateless.
So here it doesn't even apply.
So we don't even have to consider this Because the state context comes into picture only for the intermediate
operation.
In this example, the intermediate operations are just map.
I would like to explain more about it.
So we will go to the example that we coded previously and then discuss more on it.
So I'm going to open a streams flatMap example here.
Let me put this in presentation mode.
So in here.
So this is the example which we discussed as part of the presentation.
But I would like to give you some more code comments and then make it clear.
So here we are performing didn't get activities.
This is of type list is going to convert a stream of student to stream of list of string.
So this is a stateless operation.
And then here we are flattening the stream.
Basically we are converting a stream of list of string to stream of string.
It is again stateless.
So here is where we are going to perform the distinct operation.
Basically distinct operation is going to remove the duplicates right.
So this is a stateful operation.
It needs to know about the previously processed elements.
So that if it is a duplicate it can remove that element from the result.
And next thing is a sorted sorted here is going to sort the result in the ascending order.
So it needs to know the state of the previously processed elements so that it can sort accordingly as
per the current element.
And the next one is a terminal operation which is going to perform the collect.
And then it is going to give you the list of unique activities as a result.
So this is all about the stateful and stateless functions.
With this we came to the end of this tutorial.
Thank you for watching.