WEBVTT

00:00.110 --> 00:00.680
Hi everyone.

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

00:02.000 --> 00:06.170
In this tutorial we will discuss about the terminal operations.

00:07.040 --> 00:12.110
Terminal operations are the ones that collects the data for you in the Streams API.

00:12.800 --> 00:16.910
Terminal operations starts the whole stream pipeline.

00:17.030 --> 00:24.200
Some of the terminal operations are for each min max reduce, collect and et-cetera.

00:25.580 --> 00:32.720
In this section, the main focus is going to be on the collect terminal operation and we'll be focusing

00:32.720 --> 00:38.360
on what are the different approaches of collecting the data using the collect method.

00:38.390 --> 00:42.200
The collect method takes in an input of type collector.

00:43.160 --> 00:48.500
It produces a result as per the input passed to the collect method.

00:48.530 --> 00:54.260
Basically, the collect method is going to be like an accumulator, which is going to accumulate the

00:54.260 --> 00:57.830
result until the streams are exhausted.

00:59.550 --> 01:03.510
The first terminal operation that we are going to talk about is joining.

01:04.450 --> 01:11.300
Joining is used to perform the string concatenation on the elements in the stream.

01:11.370 --> 01:14.800
So joining will be an input to the collect method.

01:15.370 --> 01:18.640
Joining has three different overloaded versions.

01:18.670 --> 01:22.270
Let's go ahead and explore all of those in this tutorial.

01:22.300 --> 01:23.410
Back to the IntelliJ.

01:25.170 --> 01:28.740
And in here what I'm going to do, I'm going to create a package called.

01:29.880 --> 01:30.780
Dreams.

01:32.010 --> 01:34.110
Underscore terminal.

01:36.370 --> 01:41.590
And then what I'm going to do, I'm going to create an example called dreams.

01:43.170 --> 01:45.180
Joining example.

01:45.630 --> 01:49.080
Let's make this class executable by adding public static void main.

01:49.560 --> 01:50.370
Here we go.

01:54.420 --> 01:59.370
The use case that we are going to code here is that we want to concatenate all the names that are part

01:59.370 --> 02:03.150
of the student database and return that as a result.

02:03.670 --> 02:06.630
So I'm going to create a method called public static.

02:07.020 --> 02:10.620
We all know joining is going to return you a string.

02:10.920 --> 02:15.720
I'm going to have the return type A string for this method, and then I'm going to name this one as

02:15.720 --> 02:21.540
joining underscore one, because there is going to be three different overloaded versions of joining.

02:21.690 --> 02:24.540
That's the reason why I'm naming it as one for now.

02:25.020 --> 02:31.300
First up is let's call the student database dot, get all students dot stream.

02:31.320 --> 02:38.790
The first call to that method is stream is going to give us a stream of students followed by that we

02:38.790 --> 02:40.250
want just the name, right?

02:40.260 --> 02:49.170
So I'm going to use the map method and then call the get name method, which is going to give me the

02:49.170 --> 02:49.930
names.

02:49.950 --> 02:55.140
Basically, this is going to return a stream of string.

02:56.290 --> 02:57.130
As an output.

02:57.130 --> 03:00.790
And then what we are going to do, we are going to call the collect method.

03:00.970 --> 03:04.210
The collect method is going to accept a collector.

03:05.880 --> 03:08.610
Collectors basically dot joining.

03:10.400 --> 03:10.790
Right.

03:10.790 --> 03:12.410
And then I'm going to return this year.

03:15.880 --> 03:18.130
Let's run this and then check the result.

03:18.450 --> 03:21.370
Going to have dining one here.

03:23.780 --> 03:24.950
Then call this method.

03:26.710 --> 03:29.270
Let's run this example and then check what is the result.

03:29.300 --> 03:34.840
So here we are calling the joining method and then we are printing the value using this result.

03:37.700 --> 03:41.860
So here it printed the name in concatenation.

03:41.870 --> 03:45.890
Here it printed the concatenated name of all the students.

03:45.890 --> 03:48.710
But if you take a look at it, it is not readable.

03:48.710 --> 03:49.190
Right?

03:49.230 --> 03:51.740
So there is an overloaded version of this one.

03:51.890 --> 03:56.240
What I'm going to do, I'm going to add joining underscore to.

03:56.460 --> 03:58.940
So in this, you can pass the delimiter also.

03:59.270 --> 04:08.870
If I pass this, basically this is going to create a delimited string values in here finding to going

04:08.870 --> 04:17.330
to be renamed as joining to run this it is going to print all the values by injecting the delimiter

04:17.330 --> 04:19.730
between each and every variable.

04:19.760 --> 04:22.670
Now, this is in a readable format.

04:22.700 --> 04:26.170
There is an another version of joining that is available here.

04:26.210 --> 04:27.620
Let's go ahead and take a look at it.

04:28.940 --> 04:29.900
You take a look at it.

04:31.600 --> 04:36.520
This is going to be joining three and it accepts a prefix and suffix.

04:36.910 --> 04:42.730
So you can give this is a prefix and then this is a suffix.

04:42.760 --> 04:49.000
Basically, this whole name string is going to have this as a prefix and this as a suffix.

04:49.120 --> 04:51.340
Let's go ahead and run this and check this one.

04:53.120 --> 04:54.440
Going to be joining three.

04:55.690 --> 04:57.400
And this is going to be joining three.

04:57.730 --> 04:59.200
Let's go ahead and run this one.

05:00.870 --> 05:03.120
This result is going to be.

05:03.630 --> 05:04.170
Take a look at it.

05:04.170 --> 05:06.660
It has the brackets, meaning it has the braces.

05:07.780 --> 05:09.160
Here and it has the.

05:10.170 --> 05:10.910
Races here.

05:10.920 --> 05:16.920
Basically, this is a prefix, the value that you passed here and this is a suffix, the value that

05:16.920 --> 05:17.460
we passed.

05:18.930 --> 05:24.270
Before we wind up this tutorial, I would like to show you another approach of using joining here.

05:24.270 --> 05:27.690
If you notice this, we are using collectors dot joining, right?

05:27.690 --> 05:31.140
But actually you don't have to use the collectors all the time.

05:31.140 --> 05:33.360
You can do a static import of joining.

05:33.360 --> 05:39.630
I'm pressing the alt and enter and Mac in the case of windows you have the alt plus enter.

05:39.630 --> 05:44.760
Basically we have added the static import of collectors with this.

05:44.760 --> 05:50.790
It is going to perform the same operation as you perform using the collectors dot joining.

05:50.940 --> 05:54.150
So this is all about the joining collector.

05:54.180 --> 06:00.360
Let's talk about some more collectors in the future tutorials with this we came to the end of this tutorial.

06:00.390 --> 06:01.560
Thank you for watching.
