Hi everyone.
Welcome back to this tutorial.
In this tutorial, I'll be explaining about the factory methods that are part of the Steam API.
The factory methods are of generate and iterate.
Let's go ahead and take a look at the off function first.
So our function is used to create a stream of certain values passed to this method.
Let's take a look at the example.
So in this example we have the stream, which has a static method called off, which takes certain values.
In this case it is, it is taking a string.
Adam, Dan and Julie, this is going to give you a stream of string as an output.
So we have seen examples of creating a stream out of the list.
This is one another approach of creating a stream using the off method.
Iterate and generate are used to create infinite streams.
So let's take a look at the example of iterate first.
So streams dot iterate.
Again, this is a static method.
This is the initial value, which takes two arguments actually.
The first argument represents the initial value.
The second argument is going to give you.
So in this example, it is going to start from one and it is going to iterate infinite times.
And every time this method is executed is going to give you the value as one into two, two into two,
three into two as an output.
Let's take a look at the generate example here.
The generate basically takes in a supplier.
We all know supplier by now, supplier doesn't take any input, but it is going to return you an output.
Let's go ahead and code this one and then explore all these methods.
Ardently open what I'm going to do.
I'm going to create an example.
Called Stream.
Of generate.
It rate example.
And let me make this class executable by adding the public static void main method.
Put this in presentation mode.
The first purpose.
We are going to create the stream using the off method stream dot off method.
As you see here, I have this off method.
I'm going to pass in values like Adam, Dan.
Julie.
That's it.
So this is going to return a stream of string as an output.
And then I'm going to name this one as string stream.
So let's go to this off method.
Go.
If you take a look at the off method, it takes an infinite number of values and then it is going to
return a stream of type string.
So whatever type that you're passing, that is a type that it is going to return.
So what we will do, we will just iterate.
The for you for each method.
System.out.println.
Run this example that is going to print all the values in the console.
There you go.
It printed the Adam, Dan, Julie.
This is one way of creating a stream of the values that you can pass as an input to this off method.
Now let's explore the iterate method.
I'm going to type in the stream class dot and then we are going to make use of the iterate method.
We go to the iterate method.
Iterate method takes in two input.
One is the seed, which is the initial value, and then the unary operator, the unary operator.
We all know by now it is going to take an input of one type and then it is going to return the output
of the same type.
So I'm going to have the initial value as one, and then I want to perform a multiplication of two for
each value that it is going to return.
And then I'm going to do for each it's going to be system dot out, dot println.
I want to print and then show you how the values look like first and then we'll explore some additional
enhancements for this particular iterate method.
Now run this.
I'm going to stop this one.
So here it is, printing zero.
Let's go to the top of the console and then take a look at what's happening there.
So it printed Adam, Dan, Julie.
That's what we are doing here.
And then if you take a look at it, it printed the initial value and the following value is the multiplication
of that initial value into two, followed by that the previous value into two, the previous value into
two.
It continues until it reaches the max value of an integer.
So here, this is an integer, right?
So it reaches this max value and after that it is just printing zero.
The reason why this is happening is because we are iterating in finite times.
So there is a handy function which is part of the Stream API which we covered in one of the previous
tutorial.
You can use the limit to limit the number of values it can pick from the stream and then perform this
iteration.
So in here I'm going to have the value as ten.
Basically, this is going to limit this iterate to just pass ten values and then it's going to stop
the infinite stream.
If I run this example that is just going to take in only the first ten values from this iterate method
and then it's going to print the values and it is going to stop the stream.
So this is all about the iterate method.
The next step is we'll go ahead and explore the generate method.
So if we type in the stream class, it has the generate method and as you see, it is taking a supplier
as an input.
What I'm going to do, I'm going to create a supplier instance and this is going to be of type integer.
I'm going to just name it as integer supplier.
In here, what we are going to do, we are going to make use of the random class, which is part of
the Java API itself.
And we are going to.
Let's import this first.
So what are the methods it supports?
There is this method called next end, which is going to give you some random values.
Let's make use of that.
So generate.
And in here I'm going to pass the supplier.
A player.
Sorry.
Integers, a player.
That's it.
What it is going to do.
It is going to generate again infinite streams.
I would like to print this first and then show you what's happening there.
And then we'll use a limit function to limit the values, to limit the values from the generate method.
I'm going to run this.
There you go.
It printed all these random integers which was generated by this next int method.
So I want to limit this for just five values so that we can see what's happening there.
There you go.
It just printed the Pi values.
All these values came from the next INT method, which is randomly generating an integer using the random
class.
So this is all about the of iterate and generate method.
You can use this method whenever you need an on demand stream based on your use case, you can provide
the values or you can implement your own functionalities.
Here.
With this, we came to the end of this tutorial.
Thank you for watching.