Hi everyone.
Welcome back to this tutorial.
In this tutorial I will compare the collections with streams and highlight some of the key differences
between them.
As the table denotes.
The left hand side represents a collection, and the right hand side represents the stream.
In collections.
We can add, remove and modify elements.
That is not the case with streams you cannot modify or you cannot add elements to the stream.
Once you have the stream created.
Let's go ahead and run this by an example.
Have the IntelliJ open in here.
What I'm going to do, I'm going to create a class called Collection.
Elections versus string.
Want to make this class executable?
Public static void Main.
I want to put this in presentation mode so that the code will be clear.
The first step, which I'm going to do, I'm going to create another list, which is a collection.
Spring, then I'm going to name it as names.
Let's import this.
This one is going to be a new array list.
So we have the instance ready.
The next step is I'm going to add some elements to it.
Name Dot Add.
I'm going to add the name as Adam.
The second name is going to be Jim.
The third name is going to be Jenny.
So.
I created the collection here and then I added the elements here if we want to remove.
Let's say I want to remove the element in the first index, I'm going to pass zero.
Then if I do a print.
It's going to.
And only the two names, Jim and Jenny.
So in collections you can add, you can remove or you can modify the values also.
That is not the case with streams.
What I'm going to do, I'm going to create a stream names, dot stream.
As soon as I created this, it's going to give me access to all the stream methods it support.
It's a Is there a method called Add?
There are a method called remove.
So scream doesn't allow you to add, modify or delete elements from the stream once it is created.
That is the first thing which.
We have to register in our mind.
Let's go back to the presentation and talk about the remaining points in the table.
The next point in the comparison is about accessing the elements from the collection and stream in collections.
The elements can be accessed in any order.
Let's say you have an array list of five elements and you want to check what's there in the fifth element.
Then in that case you will use list dot get and you pass the index.
So array list index starts from zero.
You pass the index value as four, then it is going to give you the fifth element in that list.
That is not the case in streams.
In streams you can only access the elements one by one in a sequence.
The next point is elections is eagerly constructed and streams are lazily constructed.
What does it mean?
It means that you have to populate the collection with the values if you want to manipulate it.
That is not the case with strings unless and until a terminal operation is invoked.
The stream doesn't starts its process at all.
We saw that in our previous tutorial.
The next one is an important point.
Elections can be traversed any number of times.
That is not the case with streams.
It can be traversed only once.
Let's take a look at an example of this one.
I'm going to use the same example that we quoted.
There you go.
So in here.
Let's say you want to iterate this collection, what do we do?
We create a for loop.
Spring, want to give it a name and then pass the collection.
And then if we want to print all the names, we just print the name.
There you go.
So we printed all the names.
We want to iterate the collection again.
I can do that.
The had printed the names again.
That is not the case with streams.
I'll give you an example of that.
So I'm going to create a stream names, dot stream.
Basically, this stream is of type collection of type list, which holds a type as string.
So this is going to return me a stream of string.
To name this one as name stream.
And I'm going to import this.
There you go.
This is what it represents.
Now, if I want to iterate, right, so what I'm going to do, I'm going to do name stream dot for each
and I'm going to print the value.
System.
Dot out Colon.
Colon print.
Ellen I'm using the method reference syntax.
So if I run this program, it's going to work.
It's going to print all the results here.
It's good if we want to iterate the stream again.
Right.
We want to do the same operation again.
If I run the program, you'll be having some exception here.
The exception here is Java Dot Lang.
Illegal state exception stream has already been operated upon or closed.
Basically you have a stream created here and with this stream you can use it only once.
That's the meaning of you can only traverse the stream only once.
Let's go back to the next point.
It's about how iteration happens in collections and stream collections, perform external iteration
to iterate through all the elements.
Streams perform internal iteration to iterate through all the elements.
What does it mean?
So in this example, we have explicitly created this for loop and we are instructing that to iterate
through the collection and print all the values in here.
We didn't code anything for it, we just use the method call for each which is part of the streams API.
It performs the internal iteration for you.
That's the difference between the iteration, between collection and streams.
So this is all the differences to remember between collections and streams.
With this, we came to the end of this.