WEBVTT

00:01.240 --> 00:01.780
Hi everyone.

00:01.780 --> 00:03.100
Welcome back to this tutorial.

00:03.100 --> 00:08.440
In this tutorial I will compare the collections with streams and highlight some of the key differences

00:08.440 --> 00:09.310
between them.

00:11.300 --> 00:12.260
As the table denotes.

00:12.260 --> 00:17.510
The left hand side represents a collection, and the right hand side represents the stream.

00:17.510 --> 00:18.350
In collections.

00:18.350 --> 00:21.470
We can add, remove and modify elements.

00:21.740 --> 00:28.400
That is not the case with streams you cannot modify or you cannot add elements to the stream.

00:28.400 --> 00:31.040
Once you have the stream created.

00:31.370 --> 00:33.860
Let's go ahead and run this by an example.

00:34.710 --> 00:36.690
Have the IntelliJ open in here.

00:36.690 --> 00:42.420
What I'm going to do, I'm going to create a class called Collection.

00:43.970 --> 00:47.250
Elections versus string.

00:48.180 --> 00:50.000
Want to make this class executable?

00:50.690 --> 00:52.040
Public static void Main.

00:53.040 --> 00:56.270
I want to put this in presentation mode so that the code will be clear.

00:58.080 --> 01:02.940
The first step, which I'm going to do, I'm going to create another list, which is a collection.

01:04.330 --> 01:07.800
Spring, then I'm going to name it as names.

01:07.810 --> 01:09.250
Let's import this.

01:10.420 --> 01:13.380
This one is going to be a new array list.

01:13.980 --> 01:15.600
So we have the instance ready.

01:15.610 --> 01:17.970
The next step is I'm going to add some elements to it.

01:17.980 --> 01:19.510
Name Dot Add.

01:19.960 --> 01:21.670
I'm going to add the name as Adam.

01:23.870 --> 01:25.460
The second name is going to be Jim.

01:26.120 --> 01:27.860
The third name is going to be Jenny.

01:28.400 --> 01:29.270
So.

01:30.100 --> 01:34.780
I created the collection here and then I added the elements here if we want to remove.

01:35.140 --> 01:40.150
Let's say I want to remove the element in the first index, I'm going to pass zero.

01:41.460 --> 01:42.840
Then if I do a print.

01:46.210 --> 01:47.170
It's going to.

01:48.130 --> 01:50.440
And only the two names, Jim and Jenny.

01:51.100 --> 01:56.860
So in collections you can add, you can remove or you can modify the values also.

01:57.340 --> 01:59.230
That is not the case with streams.

01:59.260 --> 02:04.360
What I'm going to do, I'm going to create a stream names, dot stream.

02:04.840 --> 02:09.940
As soon as I created this, it's going to give me access to all the stream methods it support.

02:10.660 --> 02:12.700
It's a Is there a method called Add?

02:13.060 --> 02:14.530
There are a method called remove.

02:15.330 --> 02:25.710
So scream doesn't allow you to add, modify or delete elements from the stream once it is created.

02:26.010 --> 02:27.660
That is the first thing which.

02:28.440 --> 02:29.880
We have to register in our mind.

02:30.780 --> 02:35.100
Let's go back to the presentation and talk about the remaining points in the table.

02:35.870 --> 02:43.820
The next point in the comparison is about accessing the elements from the collection and stream in collections.

02:43.970 --> 02:46.700
The elements can be accessed in any order.

02:46.730 --> 02:52.880
Let's say you have an array list of five elements and you want to check what's there in the fifth element.

02:52.970 --> 02:57.380
Then in that case you will use list dot get and you pass the index.

02:57.380 --> 03:00.110
So array list index starts from zero.

03:00.140 --> 03:06.050
You pass the index value as four, then it is going to give you the fifth element in that list.

03:06.760 --> 03:09.110
That is not the case in streams.

03:09.130 --> 03:14.590
In streams you can only access the elements one by one in a sequence.

03:16.070 --> 03:23.030
The next point is elections is eagerly constructed and streams are lazily constructed.

03:23.060 --> 03:24.230
What does it mean?

03:25.880 --> 03:31.400
It means that you have to populate the collection with the values if you want to manipulate it.

03:31.610 --> 03:37.130
That is not the case with strings unless and until a terminal operation is invoked.

03:37.160 --> 03:40.460
The stream doesn't starts its process at all.

03:40.490 --> 03:43.160
We saw that in our previous tutorial.

03:44.400 --> 03:46.260
The next one is an important point.

03:46.650 --> 03:49.740
Elections can be traversed any number of times.

03:49.950 --> 03:52.470
That is not the case with streams.

03:52.500 --> 03:54.630
It can be traversed only once.

03:54.660 --> 03:57.480
Let's take a look at an example of this one.

03:58.370 --> 04:00.650
I'm going to use the same example that we quoted.

04:02.380 --> 04:02.970
There you go.

04:02.980 --> 04:04.150
So in here.

04:05.510 --> 04:09.140
Let's say you want to iterate this collection, what do we do?

04:09.680 --> 04:10.880
We create a for loop.

04:12.120 --> 04:16.440
Spring, want to give it a name and then pass the collection.

04:16.710 --> 04:21.150
And then if we want to print all the names, we just print the name.

04:22.490 --> 04:22.870
There you go.

04:24.590 --> 04:26.690
So we printed all the names.

04:26.750 --> 04:29.240
We want to iterate the collection again.

04:29.840 --> 04:30.770
I can do that.

04:32.850 --> 04:34.410
The had printed the names again.

04:34.650 --> 04:36.760
That is not the case with streams.

04:36.780 --> 04:38.210
I'll give you an example of that.

04:38.760 --> 04:43.290
So I'm going to create a stream names, dot stream.

04:43.740 --> 04:51.150
Basically, this stream is of type collection of type list, which holds a type as string.

04:51.480 --> 04:55.620
So this is going to return me a stream of string.

04:58.690 --> 05:01.000
To name this one as name stream.

05:03.270 --> 05:05.070
And I'm going to import this.

05:05.100 --> 05:05.560
There you go.

05:05.580 --> 05:07.320
This is what it represents.

05:07.350 --> 05:14.640
Now, if I want to iterate, right, so what I'm going to do, I'm going to do name stream dot for each

05:14.910 --> 05:16.470
and I'm going to print the value.

05:17.750 --> 05:18.320
System.

05:18.320 --> 05:20.750
Dot out Colon.

05:20.750 --> 05:21.800
Colon print.

05:21.950 --> 05:25.490
Ellen I'm using the method reference syntax.

05:26.390 --> 05:29.240
So if I run this program, it's going to work.

05:29.240 --> 05:31.250
It's going to print all the results here.

05:31.310 --> 05:34.640
It's good if we want to iterate the stream again.

05:36.220 --> 05:36.760
Right.

05:36.880 --> 05:38.820
We want to do the same operation again.

05:38.830 --> 05:42.340
If I run the program, you'll be having some exception here.

05:42.370 --> 05:45.940
The exception here is Java Dot Lang.

05:45.970 --> 05:51.430
Illegal state exception stream has already been operated upon or closed.

05:51.610 --> 05:58.090
Basically you have a stream created here and with this stream you can use it only once.

05:58.090 --> 06:02.830
That's the meaning of you can only traverse the stream only once.

06:03.860 --> 06:05.390
Let's go back to the next point.

06:07.220 --> 06:15.430
It's about how iteration happens in collections and stream collections, perform external iteration

06:15.430 --> 06:17.680
to iterate through all the elements.

06:18.280 --> 06:22.620
Streams perform internal iteration to iterate through all the elements.

06:22.630 --> 06:23.680
What does it mean?

06:23.800 --> 06:31.210
So in this example, we have explicitly created this for loop and we are instructing that to iterate

06:31.210 --> 06:34.720
through the collection and print all the values in here.

06:35.410 --> 06:41.290
We didn't code anything for it, we just use the method call for each which is part of the streams API.

06:41.560 --> 06:45.010
It performs the internal iteration for you.

06:46.200 --> 06:50.870
That's the difference between the iteration, between collection and streams.

06:51.880 --> 06:56.890
So this is all the differences to remember between collections and streams.

06:57.400 --> 06:58.780
With this, we came to the end of this.
