Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will explore some more functions of streams API.
The functions are going to be distinct count and sorted distinct returns.
A unique set of elements from the stream count returns along with a total number of elements in that
particular stream sort.
It is going to sort the elements in the stream.
Let's go ahead and code these scenarios.
Then here what I'm going to do, I'm going to use the same flatmap example class in here.
I'm going to run this example and then see and then check the result basically.
So here, if you take a look at it, let's consider swimming.
Swimming is a repeated in many places because the way we have built this pipeline is get all students
and get all the activities of the students and then consolidate and build that as a list.
That's what we are doing here.
Now, let's say I want to know what are the unique set of activities that all the students are participating.
Basically, I want to know out of all these, I don't want any duplicates.
What are the unique activities that students are participating?
The way to achieve that is by calling.
Distinct.
If we call distinct, basically it is going to perform that unique operation and then return you the
result with the unique activities.
What I'm going to do.
I'm going to run this example right now.
If you take a look at this result, it has many duplicates.
So for swimming, there are many instances of that available.
In the result.
We take a look at the basketball.
There are many instance of that available in the result.
I'm going to run this now and then check the result.
So the only thing that I only change that I made here is that I added this distinct function as part
of the stream pipeline.
Run this.
There you go.
What it did was it performed the distinct operation for all the elements in that stream.
This is equivalent to the distinct that we use in the SQL queries.
So the result is that these are the unique set of activities that students are participating in that
from that student database class.
Now what I want is I want to know what are all the the total number of activities right now.
We know the unique activities.
Right.
I want to know what is the total number of activities that are supported by this school, meaning supported
by this student database.
What I'm going to do I'm going to copy this.
And then I'm going to name this one as get student.
Activities.
Don't.
Okay.
And this is going to return a long.
That's what it is going to do.
I'm going to name this one as long.
Number of.
Couldn't.
Activities.
Right.
And in here, instead of select a list, I'm going to use the method count.
Count is going to give you the total number of.
Elements in that stream.
So this is going to again return a stream of string.
But.
Distinct function.
Performed, right?
Billing mistake here with distinct, distinct function performed on top of it.
Now if I call this.
Call this one.
Now, if I call this what it is going to give, it is going to give me the total number of.
Activities that.
Students are participating.
So the total number of activities is nine.
I can go ahead and calculate this by manually.
Let's say this is one, two, three, four, five, six, 789.
I can manually do it.
But the idea here is I want the system.
I want the streams API to tell me how many distinct activities are available as part of that student
database class.
Now.
What I want is here if you take a look at the result.
The result is not in alphabetical order.
I want the result to be in alphabetical order.
In those kind of scenarios, there is a handy function which is available.
If you take a look at it, it is called sorted.
If I perform sorted, it is going to.
Sort it in natural order.
Basically, it is going to perform.
It is going to sort the result in alphabetical order.
Let's run this result and then check.
So here the result is not sorted.
As soon as I run this example, it is going to sort the result because I have added the sorted method
here.
And this example and then check.
There you go.
It sorted.
The result in the alphabetical order starts with A and ends with Y.
But these are all the handy functions which are available on stream API supports in order to get your
desired result.
With this we came to the end of this tutorial.
Thank you for watching.