CodeGym /Java Course /Java Syntax /ArrayList vs. LinkedList

ArrayList vs. LinkedList

Java Syntax
Level 8 , Lesson 5
Available

"How about a little training for your brain? I hope it's still in working order."

"Earlier, in the table of containers and collections you saw that the same interface can have multiple implementations. I'll now tell you why we need that. And how ArrayList differs from LinkedList."

"The thing is, collections may be implemented in different ways and no implementation is always ideal. In one approach, some operations are fast, but others are slow. The opposite can be true for another approach. There is no perfect one-size-fits-all solution."

"That's why the decision was made to implement several versions of the same collection. Each implementation should be optimized for some narrow set of operations. This is how different collections came to be. Let's study two classes as examples: ArrayList and LinkedList."

ArrayList vs. LinkedList - 1

"Internally, ArrayList is implemented as an ordinary array. That's why inserting an element in the middle requires that we first shift all the succeeding elements by one, and then put the new element into the free slot. Getting and setting elements (get, set) is fast, since these operations simply address the relevant array element."

"LinkedList has a different internal structure. It's implemented as a list with interconnected elements: a set of distinct elements, each of which stores references to the next and previous elements in the list. To insert an element into the middle of such a list, you only need to change the references of its future neighbors. However, to get element No. 130, you have to run through each object from 0 to 130. In other words, get and set operations will be slow. Look at the following table:"

Description Operation ArrayList LinkedList
Get an element get Fast Slow
Set an element set Fast Slow
Add an element (to the end of the list) add Fast Fast
Insert an element (at an arbitrary position) add(i, value) Slow Fast
Remove an element remove Slow Fast

"I see. I'm starting to understand it now. Are there any criteria or rules that would help me know which collection is best in a particular situation?"

"To keep it simple, I'll give you the following rule: if you are going to frequently insert (or remove) elements in the middle of the collection, it's better to use LinkedList. In all other cases, ArrayList works better."

"We'll delve into how they are structured in more advanced lessons, but for now we'll just practice using them."

Comments (12)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Fadhil Radhian Level 18, Semarang, Indonesia
27 March 2023
short and concise explanation
Tasmoda Level 28, Midrand, South Africa
16 May 2022
"if you are going to frequently insert (or remove) elements in the middle of the collection, it's better to use LinkedList". LinkedList > Iterates through the list from the beginning just to get to the desired position in the middle. ArrayList > Goes directly to the desired position without iterating the entire list, but must shift all elements after deletion or before addition of an element. Therefore, with regards to adding and deleting elements, I don't think one implementation is better over the other.
Noob_Coder Level 22, Springfield, United States Expert
8 November 2020
Is LinkedList really useful vs arraylist?I don't think so lol.
Jonaskinny Level 25, Redondo Beach, United States
17 February 2022
wait till you need to implement your own caching mechanism .... LinkedList is your friend.
Marek Pasierbek Level 9, Poznań, Poland
9 July 2020
good text, good graphics
Jakub Góźdź Level 31, Katowice, Poland
13 February 2020
Thanks, with that comparison it is much easier to understand that
ashgolan Level 8, Ashdod, Israel
14 November 2019
ok
Shndlr B Level 17, France
30 April 2019
If you're going to be inserting elements in the middle of a LinkedList won't it internally traverse half of the list? I get that there's no need to copy memory with a LinkedList but isn't jumping from object to object really inefficient?
Shndlr B Level 17, France
30 April 2019
Okay nevermind. They do address this later in an article.
Jonaskinny Level 25, Redondo Beach, United States
17 February 2022
switching references gives you an intermediary that avoids the shifting of the actual elements.
// Java Poser Level 18, Cincinnati, United States
9 March 2019
I love the Table! thank you guys at CodeGym!
Hashirama Level 26, Port-Harcourt, Nigeria
19 January 2019
This explanation is so clear.