WEBVTT

00:00.980 --> 00:01.520
Hi everyone.

00:01.520 --> 00:02.780
Welcome back to this tutorial.

00:02.780 --> 00:06.500
In this tutorial, we will explore the numeric streams.

00:06.500 --> 00:09.080
What are numeric streams in the first place?

00:09.110 --> 00:15.140
Numeric streams are used to represent the primitive values in a stream.

00:15.320 --> 00:16.640
There are three different types.

00:16.640 --> 00:20.480
One is a stream, long stream and double stream.

00:20.510 --> 00:26.150
I would like to explain the advantages of numeric stream over the regular stream.

00:26.150 --> 00:29.840
Using an example, let's go back to the IntelliJ and code this one.

00:31.770 --> 00:36.240
Then here what I'm going to do, the first step is I'm going to create a package called.

00:38.260 --> 00:39.430
Numeric streams.

00:40.350 --> 00:43.530
And then I'm going to create a class called.

00:45.440 --> 00:46.250
Numeric.

00:47.460 --> 00:48.780
Reims example.

00:50.920 --> 00:54.370
Make this class executable by adding public static void main method.

00:58.100 --> 01:03.470
So the use case that we are going to code, we are going to perform sum of N numbers.

01:03.860 --> 01:08.360
The first step is I'm going to build the input list.

01:09.140 --> 01:10.340
List of integer.

01:11.210 --> 01:12.500
Let's import this one.

01:13.940 --> 01:14.840
And then.

01:16.200 --> 01:17.460
Start an integer list.

01:18.470 --> 01:18.890
Its name.

01:18.890 --> 01:22.340
This one as arrays, dot as list.

01:22.370 --> 01:26.370
Basically, we are going to create a integer list.

01:26.390 --> 01:29.090
I'm going to provide the input values.

01:30.450 --> 01:30.960
There you go.

01:30.960 --> 01:32.640
We have the input list ready.

01:32.730 --> 01:34.350
I'm going to create a method here.

01:34.830 --> 01:41.160
The method is going to perform sum of N numbers and return the sum as a result.

01:42.510 --> 01:43.080
Some of.

01:44.140 --> 01:45.040
In numbers.

01:47.120 --> 01:49.790
And it is going to take in this input.

01:50.780 --> 01:52.040
List of integers.

01:52.070 --> 01:54.430
As a method parameter.

01:54.440 --> 01:57.740
And in here we are going to perform the summation.

01:57.740 --> 02:01.550
Here we are going to use the reduce function, which is part of the streams API.

02:01.580 --> 02:08.660
The first step is we are going to create the stream and then let's use the reduce method.

02:08.960 --> 02:14.690
The reduce method is going to take in a default value, meaning the initial value.

02:14.690 --> 02:19.880
And then we are going to perform sum of two numbers.

02:20.990 --> 02:22.220
Then X plus Y.

02:24.440 --> 02:25.880
Then I'm going to return this method.

02:28.820 --> 02:30.180
For a keyboard return.

02:31.390 --> 02:31.680
Here.

02:31.690 --> 02:33.850
Let's print the result and then check the result.

02:35.470 --> 02:38.440
Sum of numbers.

02:39.630 --> 02:41.400
And here we are going to.

02:42.380 --> 02:45.350
All this method and pass the integer list as an input.

02:46.100 --> 02:48.320
Let's run this and validate the result.

02:49.650 --> 02:50.760
Tony's run the program.

02:50.760 --> 02:56.600
It gave me the result as sum of N numbers, so we got the desired result.

02:56.610 --> 02:58.590
But there is a problem with this code.

02:58.590 --> 03:03.720
Actually, if you take a look at it, we are passing a list of integer.

03:03.720 --> 03:08.250
Integer is a wrapper class for the int primitive type.

03:08.250 --> 03:10.290
So what happens behind the scenes?

03:10.290 --> 03:19.350
Basically under the hood it performs the unboxing operation to convert the integer to int.

03:19.530 --> 03:21.570
So there is some wasted effort here.

03:21.570 --> 03:28.980
So every time it the stream passes an element it is going to perform unboxing of that integer, converting

03:28.980 --> 03:34.650
this integer wrapper class to a int primitive type and then perform the summation and return the result.

03:35.040 --> 03:37.740
So how can we avoid this?

03:37.740 --> 03:40.650
That's where the end stream comes into picture.

03:40.680 --> 03:44.370
I'm going to create a method called public static int.

03:46.340 --> 03:47.180
Some of.

03:48.050 --> 03:48.900
In numbers.

03:48.920 --> 03:49.850
INT string.

03:51.920 --> 03:56.300
So end stream as a handy method called in stream.

03:56.300 --> 03:56.630
There you go.

03:56.630 --> 04:03.200
It is part of the java.util dot stream package dot, and then it has a method called range closed.

04:03.650 --> 04:09.140
So we will cover this range closed and ranges as a separate tutorial in the next tutorial.

04:09.170 --> 04:11.960
But for now you can use this range closed.

04:11.990 --> 04:20.150
Basically this is going to give you one, two, three, four, five, six is going to give you six integers.

04:21.130 --> 04:23.980
And then we are going to perform a sub method.

04:25.720 --> 04:26.860
It has a lot of other methods.

04:26.860 --> 04:28.960
We'll be exploring all the different methods.

04:28.990 --> 04:30.220
The following tutorials.

04:30.220 --> 04:35.230
For now, our use case is going to be we are going to perform the sum of N numbers.

04:36.750 --> 04:37.890
And I'm going to return this.

04:39.670 --> 04:42.460
And if you take a look at it, one more thing here.

04:42.460 --> 04:44.050
We had to call the stream method.

04:44.080 --> 04:46.420
But in here we did not call the stream method.

04:46.450 --> 04:53.200
The reason being if you go to this range closed, the return type of this range closed is in stream.

04:53.410 --> 04:55.470
That is something which we have to keep in our mind.

04:55.480 --> 05:01.450
It already creates a stream and gives you the stream and this is going to pass the values one by one,

05:01.990 --> 05:03.820
one to.

05:05.560 --> 05:05.950
Three.

05:07.550 --> 05:08.060
For.

05:09.320 --> 05:11.080
Five and six.

05:11.090 --> 05:17.060
The Sun mother is going to get the values one by one and then perform the accumulation of the result

05:17.060 --> 05:19.640
and return the result as a output.

05:22.460 --> 05:23.750
I'm of in numbers.

05:24.950 --> 05:25.970
Using end.

05:27.210 --> 05:27.470
Bring.

05:29.940 --> 05:30.160
So.

05:31.490 --> 05:31.780
Dream.

05:31.790 --> 05:33.680
I don't have to pass any input to that method.

05:33.680 --> 05:38.150
It is just going to be an empty method, meaning empty input method.

05:38.930 --> 05:40.700
Let's run this and check the result.

05:41.740 --> 05:42.280
There you go.

05:42.310 --> 05:44.710
It printed the same result.

05:45.310 --> 05:52.720
So under scenarios where you are going to perform number related operations, then you can make use

05:52.720 --> 05:56.650
of the numeric streams instead of the regular streams.

05:56.680 --> 05:58.750
With this, we came to the end of this tutorial.

05:58.780 --> 05:59.950
Thank you for watching.
