Hi everyone.
Welcome back to this tutorial.
In this tutorial we will discuss about the terminal operations.
Terminal operations are the ones that collects the data for you in the Streams API.
Terminal operations starts the whole stream pipeline.
Some of the terminal operations are for each min max reduce, collect and et-cetera.
In this section, the main focus is going to be on the collect terminal operation and we'll be focusing
on what are the different approaches of collecting the data using the collect method.
The collect method takes in an input of type collector.
It produces a result as per the input passed to the collect method.
Basically, the collect method is going to be like an accumulator, which is going to accumulate the
result until the streams are exhausted.
The first terminal operation that we are going to talk about is joining.
Joining is used to perform the string concatenation on the elements in the stream.
So joining will be an input to the collect method.
Joining has three different overloaded versions.
Let's go ahead and explore all of those in this tutorial.
Back to the IntelliJ.
And in here what I'm going to do, I'm going to create a package called.
Dreams.
Underscore terminal.
And then what I'm going to do, I'm going to create an example called dreams.
Joining example.
Let's make this class executable by adding public static void main.
Here we go.
The use case that we are going to code here is that we want to concatenate all the names that are part
of the student database and return that as a result.
So I'm going to create a method called public static.
We all know joining is going to return you a string.
I'm going to have the return type A string for this method, and then I'm going to name this one as
joining underscore one, because there is going to be three different overloaded versions of joining.
That's the reason why I'm naming it as one for now.
First up is let's call the student database dot, get all students dot stream.
The first call to that method is stream is going to give us a stream of students followed by that we
want just the name, right?
So I'm going to use the map method and then call the get name method, which is going to give me the
names.
Basically, this is going to return a stream of string.
As an output.
And then what we are going to do, we are going to call the collect method.
The collect method is going to accept a collector.
Collectors basically dot joining.
Right.
And then I'm going to return this year.
Let's run this and then check the result.
Going to have dining one here.
Then call this method.
Let's run this example and then check what is the result.
So here we are calling the joining method and then we are printing the value using this result.
So here it printed the name in concatenation.
Here it printed the concatenated name of all the students.
But if you take a look at it, it is not readable.
Right?
So there is an overloaded version of this one.
What I'm going to do, I'm going to add joining underscore to.
So in this, you can pass the delimiter also.
If I pass this, basically this is going to create a delimited string values in here finding to going
to be renamed as joining to run this it is going to print all the values by injecting the delimiter
between each and every variable.
Now, this is in a readable format.
There is an another version of joining that is available here.
Let's go ahead and take a look at it.
You take a look at it.
This is going to be joining three and it accepts a prefix and suffix.
So you can give this is a prefix and then this is a suffix.
Basically, this whole name string is going to have this as a prefix and this as a suffix.
Let's go ahead and run this and check this one.
Going to be joining three.
And this is going to be joining three.
Let's go ahead and run this one.
This result is going to be.
Take a look at it.
It has the brackets, meaning it has the braces.
Here and it has the.
Races here.
Basically, this is a prefix, the value that you passed here and this is a suffix, the value that
we passed.
Before we wind up this tutorial, I would like to show you another approach of using joining here.
If you notice this, we are using collectors dot joining, right?
But actually you don't have to use the collectors all the time.
You can do a static import of joining.
I'm pressing the alt and enter and Mac in the case of windows you have the alt plus enter.
Basically we have added the static import of collectors with this.
It is going to perform the same operation as you perform using the collectors dot joining.
So this is all about the joining collector.
Let's talk about some more collectors in the future tutorials with this we came to the end of this tutorial.
Thank you for watching.