WEBVTT

00:00.720 --> 00:01.230
Hi everyone.

00:01.230 --> 00:02.460
Welcome back to this tutorial.

00:02.460 --> 00:08.190
In this tutorial we will code and explore the counting Collector as part of this tutorial.

00:09.410 --> 00:16.760
The name counting collector returns the total number of elements the collector collected as a result.

00:17.030 --> 00:18.470
Let's go ahead and explore this.

00:19.320 --> 00:21.120
Using our example app.

00:21.150 --> 00:23.820
Then you open here what I'm going to do.

00:23.850 --> 00:30.420
I'm going to create a class called Streams Counting example.

00:31.860 --> 00:39.390
Make this class executable by adding public static void main method and then put this in presentation

00:39.390 --> 00:39.960
mode.

00:39.990 --> 00:45.480
So here what I'm going to do, I'm going to create a method called public static.

00:45.510 --> 00:52.800
The counting method returns a type long and then I'm going to name this one as count.

00:53.580 --> 01:00.030
The first call that I'm going to make is student database dot get all students.

01:00.270 --> 01:02.460
The next one is let's call the stream method.

01:03.620 --> 01:08.260
Want to give us a stream of students for us and then we're going to use the collect.

01:09.440 --> 01:11.180
And we are going to use counting.

01:13.220 --> 01:17.030
Basically, we have to do a static import of counting.

01:17.510 --> 01:18.270
There you go.

01:18.290 --> 01:19.610
Collectors dot counting.

01:20.600 --> 01:21.920
We do a return.

01:23.060 --> 01:28.340
This is going to return total number of elements that reach the collect method.

01:28.790 --> 01:32.510
But we are we know by now here we don't have any conditions.

01:32.510 --> 01:37.910
Whatever the values that are written here, whatever the number of students that are returned here will

01:37.910 --> 01:40.050
be, the value that the count is going to return.

01:40.070 --> 01:42.620
You might be wondering, okay, there is no special case.

01:42.620 --> 01:47.180
I can do that by using the get all student dot size that is going to do the same, Right?

01:47.180 --> 01:53.210
But we can add some enhancements to it so that it will make more sense on scenarios when you can use

01:53.210 --> 01:53.930
counting.

01:54.920 --> 01:56.600
So I'm going to print this.

01:57.110 --> 02:00.620
Let's call this count method run.

02:00.620 --> 02:08.690
This is going to give me the result as six because the get all student has a total of six students.

02:08.990 --> 02:11.330
Now let's enhance this use case.

02:11.540 --> 02:12.890
How can we enhance this?

02:13.100 --> 02:18.950
Let's say I want to filter the students by their GPA.

02:19.370 --> 02:21.060
Student dot.

02:21.060 --> 02:22.380
Student dot.

02:22.890 --> 02:25.890
Get GPA greater than or equal to 3.9.

02:26.040 --> 02:33.030
The students who are as the GPA greater than 3.9 are the students, which will cross this filter layer

02:33.030 --> 02:35.340
and reach the collect method.

02:35.520 --> 02:41.140
Let's run this example and then check what is the result of the count method.

02:41.160 --> 02:42.150
The result is three.

02:42.180 --> 02:45.710
So under what scenarios you might use counting.

02:45.720 --> 02:48.030
Let's say you have a use case you want to check.

02:48.360 --> 02:51.240
You have this many number of elements.

02:52.060 --> 02:55.510
Filter this many number of elements matching this criteria.

02:55.510 --> 02:59.320
In those kind of scenarios, you can use the counting method.

03:00.480 --> 03:03.240
So this is all about the counting collector method.

03:03.270 --> 03:05.350
With this, we came to the end of this tutorial.

03:05.370 --> 03:06.390
Thank you for watching.
