WEBVTT

00:00.870 --> 00:01.440
Hi everyone.

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

00:02.790 --> 00:07.650
In this tutorial, we will explore some more functions of streams API.

00:07.680 --> 00:13.470
The functions are going to be distinct count and sorted distinct returns.

00:13.500 --> 00:20.520
A unique set of elements from the stream count returns along with a total number of elements in that

00:20.520 --> 00:22.560
particular stream sort.

00:22.560 --> 00:25.230
It is going to sort the elements in the stream.

00:25.320 --> 00:27.780
Let's go ahead and code these scenarios.

00:29.160 --> 00:35.490
Then here what I'm going to do, I'm going to use the same flatmap example class in here.

00:36.000 --> 00:40.050
I'm going to run this example and then see and then check the result basically.

00:40.080 --> 00:43.740
So here, if you take a look at it, let's consider swimming.

00:43.740 --> 00:52.950
Swimming is a repeated in many places because the way we have built this pipeline is get all students

00:52.950 --> 00:59.820
and get all the activities of the students and then consolidate and build that as a list.

00:59.820 --> 01:01.260
That's what we are doing here.

01:01.260 --> 01:09.240
Now, let's say I want to know what are the unique set of activities that all the students are participating.

01:09.240 --> 01:13.530
Basically, I want to know out of all these, I don't want any duplicates.

01:13.560 --> 01:17.370
What are the unique activities that students are participating?

01:17.400 --> 01:21.210
The way to achieve that is by calling.

01:22.350 --> 01:22.980
Distinct.

01:23.010 --> 01:31.230
If we call distinct, basically it is going to perform that unique operation and then return you the

01:31.230 --> 01:33.480
result with the unique activities.

01:33.510 --> 01:34.210
What I'm going to do.

01:34.230 --> 01:36.780
I'm going to run this example right now.

01:36.780 --> 01:40.020
If you take a look at this result, it has many duplicates.

01:40.020 --> 01:43.590
So for swimming, there are many instances of that available.

01:43.590 --> 01:44.460
In the result.

01:44.510 --> 01:45.960
We take a look at the basketball.

01:45.960 --> 01:48.960
There are many instance of that available in the result.

01:48.960 --> 01:51.510
I'm going to run this now and then check the result.

01:51.600 --> 01:57.900
So the only thing that I only change that I made here is that I added this distinct function as part

01:57.900 --> 01:59.220
of the stream pipeline.

01:59.980 --> 02:00.790
Run this.

02:01.570 --> 02:02.500
There you go.

02:02.530 --> 02:09.190
What it did was it performed the distinct operation for all the elements in that stream.

02:09.190 --> 02:13.430
This is equivalent to the distinct that we use in the SQL queries.

02:13.450 --> 02:20.650
So the result is that these are the unique set of activities that students are participating in that

02:20.650 --> 02:22.330
from that student database class.

02:22.510 --> 02:28.270
Now what I want is I want to know what are all the the total number of activities right now.

02:28.270 --> 02:31.810
We know the unique activities.

02:31.810 --> 02:32.360
Right.

02:32.380 --> 02:38.530
I want to know what is the total number of activities that are supported by this school, meaning supported

02:38.530 --> 02:40.360
by this student database.

02:40.600 --> 02:43.000
What I'm going to do I'm going to copy this.

02:45.040 --> 02:49.250
And then I'm going to name this one as get student.

02:50.240 --> 02:51.410
Activities.

02:52.240 --> 02:52.540
Don't.

02:53.290 --> 02:53.830
Okay.

02:54.280 --> 02:57.010
And this is going to return a long.

02:57.220 --> 02:58.630
That's what it is going to do.

03:00.290 --> 03:03.580
I'm going to name this one as long.

03:06.280 --> 03:06.990
Number of.

03:09.010 --> 03:09.700
Couldn't.

03:10.850 --> 03:11.630
Activities.

03:12.930 --> 03:13.350
Right.

03:13.350 --> 03:18.900
And in here, instead of select a list, I'm going to use the method count.

03:19.840 --> 03:22.810
Count is going to give you the total number of.

03:25.040 --> 03:26.330
Elements in that stream.

03:26.490 --> 03:30.500
So this is going to again return a stream of string.

03:32.540 --> 03:33.080
But.

03:36.460 --> 03:37.960
Distinct function.

03:42.880 --> 03:43.960
Performed, right?

03:46.950 --> 03:51.180
Billing mistake here with distinct, distinct function performed on top of it.

03:51.210 --> 03:53.310
Now if I call this.

03:55.460 --> 03:56.380
Call this one.

03:56.390 --> 04:03.440
Now, if I call this what it is going to give, it is going to give me the total number of.

04:04.230 --> 04:05.550
Activities that.

04:06.680 --> 04:08.050
Students are participating.

04:08.060 --> 04:11.210
So the total number of activities is nine.

04:11.240 --> 04:15.440
I can go ahead and calculate this by manually.

04:16.010 --> 04:21.530
Let's say this is one, two, three, four, five, six, 789.

04:21.560 --> 04:22.820
I can manually do it.

04:22.820 --> 04:25.670
But the idea here is I want the system.

04:25.670 --> 04:32.180
I want the streams API to tell me how many distinct activities are available as part of that student

04:32.180 --> 04:33.230
database class.

04:33.260 --> 04:34.190
Now.

04:35.010 --> 04:38.220
What I want is here if you take a look at the result.

04:38.250 --> 04:41.010
The result is not in alphabetical order.

04:41.010 --> 04:44.490
I want the result to be in alphabetical order.

04:44.490 --> 04:48.870
In those kind of scenarios, there is a handy function which is available.

04:49.050 --> 04:51.180
If you take a look at it, it is called sorted.

04:51.450 --> 04:54.300
If I perform sorted, it is going to.

04:55.740 --> 04:57.870
Sort it in natural order.

04:57.870 --> 05:00.000
Basically, it is going to perform.

05:00.960 --> 05:03.030
It is going to sort the result in alphabetical order.

05:03.030 --> 05:04.830
Let's run this result and then check.

05:04.830 --> 05:07.050
So here the result is not sorted.

05:07.050 --> 05:11.610
As soon as I run this example, it is going to sort the result because I have added the sorted method

05:11.610 --> 05:12.150
here.

05:13.050 --> 05:14.610
And this example and then check.

05:15.690 --> 05:16.220
There you go.

05:16.230 --> 05:17.130
It sorted.

05:17.130 --> 05:21.240
The result in the alphabetical order starts with A and ends with Y.

05:22.860 --> 05:29.430
But these are all the handy functions which are available on stream API supports in order to get your

05:29.430 --> 05:30.990
desired result.

05:31.020 --> 05:33.330
With this we came to the end of this tutorial.

05:33.360 --> 05:34.590
Thank you for watching.
