WEBVTT

00:00.350 --> 00:00.800
Hi everyone.

00:00.800 --> 00:02.000
Welcome back to this tutorial.

00:02.000 --> 00:07.760
In this tutorial, I'll be explaining about the factory methods that are part of the Steam API.

00:07.790 --> 00:11.930
The factory methods are of generate and iterate.

00:13.820 --> 00:16.580
Let's go ahead and take a look at the off function first.

00:17.470 --> 00:23.110
So our function is used to create a stream of certain values passed to this method.

00:23.140 --> 00:24.700
Let's take a look at the example.

00:25.650 --> 00:33.210
So in this example we have the stream, which has a static method called off, which takes certain values.

00:33.210 --> 00:36.210
In this case it is, it is taking a string.

00:36.390 --> 00:43.530
Adam, Dan and Julie, this is going to give you a stream of string as an output.

00:43.530 --> 00:47.070
So we have seen examples of creating a stream out of the list.

00:47.100 --> 00:51.300
This is one another approach of creating a stream using the off method.

00:52.920 --> 00:57.330
Iterate and generate are used to create infinite streams.

00:57.900 --> 01:00.480
So let's take a look at the example of iterate first.

01:00.510 --> 01:02.520
So streams dot iterate.

01:02.520 --> 01:04.140
Again, this is a static method.

01:04.170 --> 01:07.430
This is the initial value, which takes two arguments actually.

01:07.440 --> 01:09.690
The first argument represents the initial value.

01:09.690 --> 01:12.030
The second argument is going to give you.

01:12.060 --> 01:18.270
So in this example, it is going to start from one and it is going to iterate infinite times.

01:18.270 --> 01:26.100
And every time this method is executed is going to give you the value as one into two, two into two,

01:26.100 --> 01:27.810
three into two as an output.

01:28.790 --> 01:31.220
Let's take a look at the generate example here.

01:31.640 --> 01:34.150
The generate basically takes in a supplier.

01:34.160 --> 01:40.100
We all know supplier by now, supplier doesn't take any input, but it is going to return you an output.

01:40.130 --> 01:43.430
Let's go ahead and code this one and then explore all these methods.

01:46.110 --> 01:48.630
Ardently open what I'm going to do.

01:48.660 --> 01:50.760
I'm going to create an example.

01:52.000 --> 01:53.470
Called Stream.

01:54.970 --> 01:57.580
Of generate.

01:58.690 --> 02:01.300
It rate example.

02:02.560 --> 02:06.730
And let me make this class executable by adding the public static void main method.

02:08.700 --> 02:10.170
Put this in presentation mode.

02:14.460 --> 02:15.090
The first purpose.

02:15.090 --> 02:21.340
We are going to create the stream using the off method stream dot off method.

02:21.360 --> 02:23.520
As you see here, I have this off method.

02:23.550 --> 02:28.680
I'm going to pass in values like Adam, Dan.

02:30.830 --> 02:31.610
Julie.

02:33.560 --> 02:34.060
That's it.

02:34.070 --> 02:39.920
So this is going to return a stream of string as an output.

02:40.130 --> 02:42.980
And then I'm going to name this one as string stream.

02:43.550 --> 02:45.380
So let's go to this off method.

02:45.500 --> 02:45.830
Go.

02:45.860 --> 02:50.210
If you take a look at the off method, it takes an infinite number of values and then it is going to

02:50.210 --> 02:53.080
return a stream of type string.

02:53.090 --> 02:56.870
So whatever type that you're passing, that is a type that it is going to return.

02:57.790 --> 03:00.460
So what we will do, we will just iterate.

03:02.490 --> 03:04.140
The for you for each method.

03:04.860 --> 03:06.960
System.out.println.

03:08.230 --> 03:12.450
Run this example that is going to print all the values in the console.

03:12.600 --> 03:13.080
There you go.

03:13.080 --> 03:15.170
It printed the Adam, Dan, Julie.

03:15.180 --> 03:22.230
This is one way of creating a stream of the values that you can pass as an input to this off method.

03:24.660 --> 03:26.970
Now let's explore the iterate method.

03:27.270 --> 03:32.460
I'm going to type in the stream class dot and then we are going to make use of the iterate method.

03:32.520 --> 03:33.630
We go to the iterate method.

03:33.630 --> 03:35.640
Iterate method takes in two input.

03:35.670 --> 03:41.130
One is the seed, which is the initial value, and then the unary operator, the unary operator.

03:41.130 --> 03:46.320
We all know by now it is going to take an input of one type and then it is going to return the output

03:46.320 --> 03:47.850
of the same type.

03:48.970 --> 03:56.460
So I'm going to have the initial value as one, and then I want to perform a multiplication of two for

03:56.460 --> 03:58.530
each value that it is going to return.

03:58.740 --> 04:05.880
And then I'm going to do for each it's going to be system dot out, dot println.

04:05.880 --> 04:12.360
I want to print and then show you how the values look like first and then we'll explore some additional

04:12.360 --> 04:15.900
enhancements for this particular iterate method.

04:17.160 --> 04:17.640
Now run this.

04:17.640 --> 04:18.810
I'm going to stop this one.

04:18.810 --> 04:20.280
So here it is, printing zero.

04:20.280 --> 04:23.800
Let's go to the top of the console and then take a look at what's happening there.

04:24.550 --> 04:27.250
So it printed Adam, Dan, Julie.

04:27.250 --> 04:28.840
That's what we are doing here.

04:28.960 --> 04:35.380
And then if you take a look at it, it printed the initial value and the following value is the multiplication

04:35.380 --> 04:43.030
of that initial value into two, followed by that the previous value into two, the previous value into

04:43.030 --> 04:43.210
two.

04:43.240 --> 04:47.110
It continues until it reaches the max value of an integer.

04:47.110 --> 04:48.730
So here, this is an integer, right?

04:48.730 --> 04:53.320
So it reaches this max value and after that it is just printing zero.

04:54.200 --> 04:59.900
The reason why this is happening is because we are iterating in finite times.

04:59.900 --> 05:04.340
So there is a handy function which is part of the Stream API which we covered in one of the previous

05:04.340 --> 05:05.210
tutorial.

05:05.270 --> 05:14.150
You can use the limit to limit the number of values it can pick from the stream and then perform this

05:14.180 --> 05:14.990
iteration.

05:14.990 --> 05:17.810
So in here I'm going to have the value as ten.

05:17.840 --> 05:25.160
Basically, this is going to limit this iterate to just pass ten values and then it's going to stop

05:25.550 --> 05:27.080
the infinite stream.

05:27.330 --> 05:33.710
If I run this example that is just going to take in only the first ten values from this iterate method

05:33.710 --> 05:39.020
and then it's going to print the values and it is going to stop the stream.

05:39.230 --> 05:41.870
So this is all about the iterate method.

05:41.900 --> 05:46.460
The next step is we'll go ahead and explore the generate method.

05:46.940 --> 05:53.690
So if we type in the stream class, it has the generate method and as you see, it is taking a supplier

05:53.790 --> 05:55.110
as an input.

05:55.230 --> 06:04.710
What I'm going to do, I'm going to create a supplier instance and this is going to be of type integer.

06:04.740 --> 06:07.080
I'm going to just name it as integer supplier.

06:07.110 --> 06:12.360
In here, what we are going to do, we are going to make use of the random class, which is part of

06:12.360 --> 06:13.890
the Java API itself.

06:13.890 --> 06:15.930
And we are going to.

06:17.600 --> 06:18.920
Let's import this first.

06:20.300 --> 06:22.190
So what are the methods it supports?

06:22.400 --> 06:26.630
There is this method called next end, which is going to give you some random values.

06:27.020 --> 06:28.580
Let's make use of that.

06:29.330 --> 06:31.580
So generate.

06:31.580 --> 06:33.620
And in here I'm going to pass the supplier.

06:35.640 --> 06:36.450
A player.

06:36.840 --> 06:37.170
Sorry.

06:37.170 --> 06:38.250
Integers, a player.

06:40.260 --> 06:40.800
That's it.

06:40.830 --> 06:41.970
What it is going to do.

06:42.000 --> 06:44.610
It is going to generate again infinite streams.

06:44.610 --> 06:47.970
I would like to print this first and then show you what's happening there.

06:47.970 --> 06:57.480
And then we'll use a limit function to limit the values, to limit the values from the generate method.

06:57.600 --> 06:58.800
I'm going to run this.

07:01.250 --> 07:01.670
There you go.

07:01.670 --> 07:07.660
It printed all these random integers which was generated by this next int method.

07:07.670 --> 07:13.910
So I want to limit this for just five values so that we can see what's happening there.

07:16.240 --> 07:16.720
There you go.

07:16.720 --> 07:18.610
It just printed the Pi values.

07:18.610 --> 07:26.440
All these values came from the next INT method, which is randomly generating an integer using the random

07:26.440 --> 07:27.210
class.

07:27.220 --> 07:31.090
So this is all about the of iterate and generate method.

07:31.090 --> 07:37.840
You can use this method whenever you need an on demand stream based on your use case, you can provide

07:37.840 --> 07:40.330
the values or you can implement your own functionalities.

07:40.330 --> 07:40.900
Here.

07:41.020 --> 07:43.000
With this, we came to the end of this tutorial.

07:43.000 --> 07:44.140
Thank you for watching.
