LinkedList vs ArrayList in Java - Which Should You Use?

preview_player
Показать описание

#spring #learning #springboot #springtutorial #springsecurity #developpement #java #arraylist #linkedlist

ArrayList VS LinkedList in Java

Don't Forget to
===========================================
💯 Subscribe to the youtube channel

My name is Ali and I do videos every week. so if you like my videos and content and if you are new to my channel so consider subscribing to get the newest videos every time I publish and don’t forget to give me a thumbs up so I keep producing content for you.

Before you start, just go ahead and join our community on Discord and be part of the a great developers community that we are building.

Both of LinkedList and ArrayList implement the List interface which comes with a bunch of methods like: size(), isEmpty)(, add(), get(), set(), etc… so both of these object they support the exact same methods.
This means if you use ArrayList and you want to switch to a LinkedList, this will be just fine because both of them they support the exact same operation methods.

Really good question, Yeah, the main difference between these two classes comes in the way they are implemented, and using one over the other could really impact your application performance

Yes exactly, first let me tell you how an ArrayList is implemented:
* An ArrayList uses an array as an underlying data structure
* You can create an arrays in java (give an example while editing the video) and give a fixed size, as you know in java, array sizes are fixed
* If you want to insert a new element and have a bigger size, you have to create a new array and insert the the old and the new elements in it
* So an ArrayList is doing all this work behind the scene and if more size is needed for the array it will create a new array and migrate all the elements to it and insert the new ones
* But all this work comes with a price, as of the resizing can a bit of time and resources consuming

* Let me now explain to how LinkedList works behind the scenes
* Instead of an array we have a bunch of elements called nodes and each node has a reference to next node, so the linked list start with a pointer to the first element and after that each element has a pointer to the next one.
* You can also have double linked list, means the each element is linked to the next and the previous elements and that’s how linkedLists in java are implemented

Image now you want to get an element of your list, let’s say you want to get the element number 9. Each of inkedList and ArrayList has a get method.
In behind the scenes, the way to get these two element is way different
* ArrayList has what we call a random access because it uses an arrays to store data and it is super fast to get the data and it takes the same amount of time to get any element of the list.
* In A linkedList, it is way different because because java doesn’t start in a reference in between and just grab the element, it starts from the beginning or the end of the list and start jumping to the next element and see if it is the required element, this operation is way slower for a linkedList compared to an ArrayList
* The performance of a LinkedList is when you want to add or remove elements, java will start from the beginning util it find the spot where to insert the new element, and all it needs to do is to update the pointers to the new node that we want to insert
* For an ArrayList, java has to create a band new array, initialise with empty values, copy the old values to correct spots with leaving and empty spot where we want to insert the new element

* The answer is when it comes to access and sorting data, it recommended to use ArrayLists, but when it comes to manipulate data it is recommended to user LinkedList
Рекомендации по теме
Комментарии
Автор

Thanks a lot, the explanatioin is amazing!

Aqwsaful
Автор

Great explanation! Keep up the good work 💯

kryptik