Hello.
Yes, students, today we are going to talk about implementations of the list interface, we are going
to discuss such implementations as our list vector question right away, list and stack.
You have a lot of things to learn in this lesson besides the learning of the specific classes.
We are going to answer the question when you have to use each particular implementation of list interface.
I'm going to share with you real life examples where we will use list implementations today during the
lesson I'm going to show you is a tool that will allow you to investigate the interaction of classes
in the clips.
During the lesson, we are going to compare different implementations of the list interface so that
you could understand what pros and cons of each implementations are in this lesson.
We are going to compare on the array vector Copan right away and Stack also.
We're going to learn what mocker interfaces are and what is random access interface.
So as you can see, we have a huge agenda for today.
Let's don't waste our time and start our lesson.
Brunious lessons.
You already learned what list is, but let's try to answer the question when we have to use list.
In my opinion, list implementations can cover the requirements of the widest range of business cases.
Why?
Because of the next reasons.
Support of data manipulation by Elements Index.
You may want to retrieve product from specific index in your list of products, or you may want to put
user in the list of users in the middle of the list by its index.
Or probably you always want to add the new user since the beginning of the list, but not at the end.
The possibility to use index to manage element positions gives you a lot of options for data manipulations
that will help you in different business cases.
And another reason list implementations may contain duplicates.
Sometimes you just need to have container that will store all your products, for example, and you
don't care about duplicate there.
You just need to have a list of all products here.
That's why, which is at least overset sometimes one more reason usability to create subleased.
This feature is really attractive.
One for some specific business cases.
Let me share with you one example from the real life.
We implemented mobile app for the betting tips in this app.
We received data from the server.
We store the data in the list.
There was such the main type asport prediction data.
eSport prediction was done for the specific sports event that may happen either today or yesterday or
tomorrow.
So developers created sub lists of the sport prediction for yesterday, today and tomorrow.
We created three lists instead of one by splitting the original one.
Why we could pass the one list to each widget to draw the list on the UI interface only for specific
date.
That's it, by the way.
Here is the UI of the sports app, Anderson.
You will be able to create similar apps, so don't worry.
To implement such application, you have to know implementations of the list interface.
Let's move further certain.
And again, one more features a collection interface doesn't have.
We may sort our button Tepes based on the level of probability that really improves interaction with
the app.
Moreover, I can offer user to source support.
Prediction is a high probability score or by keep also rating or by anything else, wide range of use.
And one more important reason to opt for these implementations, and this reason probably is the most
important one that is not written in any book that you read.
Engineers use list implementations and most of the times because they just don't use a set.
Documentation's developers just need some container to store multiple elements in there and treat them
as a single unit without any extra features.
OK, now we want to know when and why we opt for liste implementations.
Let's investigate list Iraqi on this slide you may use at least the interface is implemented by absoluteness
class.
Absolutely.
Tintern extends APSA collection and is extended by Absol sequential sequentially.
From this diagram you may see that our own list is extended directly from the absolute list, the same
as vector and copy and write and released and linked list is extended from absolute sequential list
stack class extends vector engineers.
Doesn't like Stack for that because that is not the least by its essence.
It is closer to the reverse.
CU does a deck interface.
We're going to run it later, but because of the backward compatibility creator subject didn't change
anything here.
Is it clear?
Because Stack in its essence implement last in, first out principle, but it extends least interface,
but not that.
Even despite this, glass will not be used very often by a during the development, you might be asked
about it during the interview.
That's why I decided to mention it here.
What is also worth to mention is the implementation of random access interface.
Today during the demo, I'm going to show you what random access interface is and what mocker interfaces
are to protect our lesson today as I just jump into the eclipse to investigate the source code that
will help us to learn the topic better understand them.
I'd like to share with you the trick that will also help you in the future during the development.
Namely, I'd like to show you how you can easily find implementations of each interface and reviews
a whole Iraqian, for example.
Here is a list interface.
I should put the cursor into the editor error and press efore button.
Or you can also make mouse right.
Click and select open type Iraqi here and we can see that the separate widget is open.
Here, here I see items that extends or implements the interface.
For example, here's our list.
We can make a left mouse click to open it and here we may find our array list.
As you can see, there are really a lot of classes over here will not run.
Each of them instead will focus only on those that you will use during your work.
Let's start from the most popular implementation of this interface that you will use in 98 percent of
cases.
That is just my assumption.
But you will tell me that if I was right, here's a release class when people are released because it's
really fast and most simple words released is array with dynamic size.
But this is in very, very simple words.
I only use regular arrays inside to store elements here.
This is a variable is called element data in my store array of objects.
That means that technically we can put everything in this array, but we can't because this field has
packaged private modifier.
OK, then you might be wondering if I only use this regular array to store elements, then it should
have fixed size, is that correct?
That is correct.
Arrays in Java have fixed size, but not the array.
Least let me show is a code that adds element to the collection.
Here is an element method in today's modification count variable first.
Don't pay attention to that right now.
After that it calls another add method.
I hold control key and press mouse left click.
OK, here is a private matter and you see the first line we have if statement here we check if s equals
to the length of the array, then we initialize our element data array with the new array.
Before we go further, let's look what this s variable is.
I hold all key and press our left key to move back to the previous position where I was.
And we see that the argument in this method is a size variable size is also one of the fields in a release
class.
Now let's get back.
I keep all key press and also press arrow right key to get back to the place in the source code where
I was before.
Now, you know, that s is a size.
What does Groom method do?
Let me answer that question.
OK.
This method calls another group method within argument and person size plus one there.
And what do we have here?
We see that in arguments that we passed to this method is used to define desired minimum capacity.
You may see has that old capacity is equal to arrest lengths and in case old capacity is more than zero.
We calculate new capacity, how it is calculated.
I suggest you to investigate by itself by going deeper and method and location change.
By the way, you might see here bitwise shift.
And it is great that by this lesson you already know how it works.
If you don't know, please review my lesson from the Java course about operators.
And after that we use a race class to copy our current theory to the one with the new capacity and we
return the reference to this array.
So now you can understand that when there is no space for new element Ansary, the values from the old
are a cockpit to the new array was the new size.
OK, but what happens if we have just created the of our type and it's empty?
Let's check the default constructor.
We can see that element data is initialized with the empty array.
This is constant empty array.
But why do we need this default capacity constant?
To answer this, let's get back to zero method.
Pay attention to the if clause here.
If old capacity more than zero.
And we know that when our only has been just created, its size is zero.
And the second clause is if Elliman data is not equal to the full capacity MHRA.
We know that this statement is also false because we initialized our rate with this constant.
So that's why our full program execution goes to the else block where we initialize our element data
array with the rate of the Langston Wighton because it will take Maximilien between ten and one during
the creation of three.
Does it make sense now as a part of your homework?
I would encourage you to go over each method in a release like we did with Add Method to understand
how Array Works program.
You might be wondered whether you can create an array list with a specific capacity.
Yes, you can arrange.
This has special constructor for that here it why you may need to create array list of different capacity.
Let me explain.
You imagine that you created array list with capacity of 10 elements.
That means when you will add 11 element and after that sextants element and so forth, each time I should
copies of values from the previous array and add them to the new array that will cause some spikes in
our performance.
Then if you know that you're going to add to your rate around one million elements right after creation
of the array, at least it is better to set capacity at the beginning to avoid some spikes in the performance.
If we specify capacity for one million elements, we will avoid case when we constantly copying arrays
and creating new objects of arrays, keeping the old arrays in our memory until garbage collector will
remove them.
Is it clear now why you might want to create an array with the initial capacity?
Great.
Let's move on.
There is also one more interesting use case.
Imagine that you added new elements to your array list and after that you removed nine thousand one
hundred elements.
OK, there is a one thousand hundred elements in your list.
But what its capacity, its capacity is still one million to remove new elements from our array.
Inside our list we may call stream to size method.
This method streams the capacity of the relist instance to the list.
Current size.
OK, now you know how the dynamic extension happens in Frailest, I also promised you to explain what
random access is.
Can you see that array list implements random access?
Let's take a look at this interface.
Can you see that this is the empty interface?
It doesn't declare any contract for any behavior.
Then why do we need it at all?
Random access interface is a marker interface.
Sometimes in you would see so-called marker interfaces.
Why is called so xcode so?
Because the only reason why they exist is to mark interface with a specific type to use this information
in the future.
Probably it is still not clear for you.
But let me show you on the example.
Here is a random access interface which is used to mark any class that supports retrieval of elements
for the constant amount of time.
That's actually one of the key features of our list is a constant amount of time for reading an element
is because of the nature of a release and how it works under the hood.
To read an element, you just need to get it from array by specific index.
And this time is usually constant.
That's why our list is marked as implementation of random access.
Now let's see where this information is used.
If we would open collection class, this is class with utility methods nonwar, it will run it in the
separate question.
Right now, I just want to show you how information about market interfaces used I press control and
you simultaneously to search through this file and I want to search for random access words and I have
around fifty eight mentions.
You can see that random access is used with instance of operator and if closes to select specific algorithms
that will work the best here in binary search method.
You may see that in case that were passed as parameter to the mass that is compatible with random access
type then indexed binary search algorithm will be applied.
If no, then iterate a binary search method will be called.
And similarly, in the other method, check for compatibility with random access interface is used to
select the best algorithm.
Right now, I believe you understood the use case and how much the interface may be used.
That's great.
According to the agenda.
Also, we're going to cover today a few more classes.
Zaya Vector, Cooperton writer released and Stack.
While covering those, I will be super fast because they are mostly the same as I released, so I would
draw your attention only to the things that are different from the released here of Extra Vector.
It is almost the same as it released the same mechanism of storing data.
Here is the element data array to store elements.
There's only one big difference between a release and vector on assets and vector a synchronized.
What does that mean?
Let's take a look at the ad method.
For example.
Can you see the key words synchronized in the method definition?
We didn't learn yet this keyword in detail, but we will we will run it in the details durians and we'll
just write in top it.
But right now you can understand this keyword as a restriction for multiple threats of execution and
the method, but body simultaneously.
That means if two threats of execution want to add element, remove element or replace element of a
specific index, they won't be able to do this simultaneously.
All these operations will be performed one after another.
That would save you from the data inconsistency inside the vector.
That's why Vector is called safe container.
Does it make sense?
OK, let's go further.
Let me open a copy right away.
List the same stories here.
Right.
Or at least is a safe version of the array list.
Then what is the difference between vector and copywriter?
At least Corporan.
Right.
The list was introduced in Java version five.
Well, Vector was introduced in the version first in version five.
Engineers come up with improvements in performance for this, let's say, version of a release.
And we can say that right writer released is more efficient from the performance standpoint.
In comparison with Vector, for example, let me open add method.
Can you see this?
There is no synchronized word here, but synchronized block instead.
The White House improvisation is implemented in this class is different from the way it is implemented
in Vector.
That is the difference between vector unreleased and Copan.
Right?
Frailest.
And now let's open this attack was, as you remember from the beginning of the lesson, Stack extends
factor.
But take a look at the map that introduced instead.
What does this remind you of your right that is almost interface of the cube.
The names of the masses are almost the same.
Engineers don't like Stack because this is concrete glass.
That means there is no flexibility in using different implementation of the same API.
It implements LIFO principle.
Last in, first out.
We are going to review this principle in the details when we get closer to the interface.
So don't worry.
Years for now, the LIFO principle is implemented in DEC interface and in different implementation of
that interface.
But Stack was introduced in Java version one and keep the promise of backwards compatibility and to
make sure that programs that are written on Java version first will be successfully executed on the
Java version fifteens.
And later this class is still here.
That's it.
Now, you know, also the difference between a release vector, right.
And released instead.
Now let's recap what we have learned today.
Today, we investigated the Iraqi of least interface.
You saw how you may investigate source code and you Iraqi of any interface in eclipse.
Also in this lesson, we learned when we have to use implementation of the interface, such as every
vector writer relist, and so that we learned in detail how size of the array maybe dynamically increased
in there.
At least now you know the difference between a real E vector right at released and stack.
During the lesson we learned what Mark interfaces are.
On the example of random access interface, we learned how market interface might be used.
That's what we have for today.
Thanks a lot for your attention and see you in the next lesson.