LinkedList vs ArrayList in Java Tutorial - Which Should You Use?

preview_player
Показать описание
Updated answer: Although the complexity analysis in this video still stands, in real world applications, ArrayList is the better choice for virtually every single use case. See the pinned comment below for more info.

LinkedLists are a great data structure to be familiar with, but they can be confusing. If you're familiar with ArrayLists in Java and how to use them, then LinkedLists can be used in the same way.

But when should you use a LinkedList vs. an ArrayList? How do you use them - are there any differences. We'll go over all of that and more in this beginner/intermediate Java video tutorial lesson.

Learn or improve your Java by watching it being coded live!

Hi, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java learners.

Let me know what else you'd like to see!

Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.

📕 THE best book to learn Java, Effective Java by Joshua Bloch

📕 One of my favorite programming books, Clean Code by Robert Martin

🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial

🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)

📹Phone I use for recording:

🎙️Microphone I use (classy, I know):

Donate with PayPal (Thank you so much!)

☕Complete Java course:

Рекомендации по теме
Комментарии
Автор

I've been working with Java for nearly 15 years now. I remember studying linked list in and various other data structures in college. John, you explained something that college professors flunked folks over so much better and in a matter of minutes rather than days. Bravo sir.

prototype
Автор

Your channel is the only one that has increased my enthusiasm for Java, tenfold. Your videos really are a breath of fresh air here on YouTube. I’m going to watch all your vids and thumb-them-up in gratitude. 👍

findlestick
Автор

I once had a job to improve performance of a java application. Best improvement was done by just exchange a LinkedList to an ArrayList, because it was used to read a lot by index. Very simple change, but massive impact.

goerekt
Автор

I don't know how your videos can be so condensed but still thorough. Thanks from all of the Computer Science majors.

jam-yhil
Автор

The arrayList does not leave a "space" for the new element in the new array. It instead duplicates all the values from the index into which you want to move the new element into . Those duplicates are positioned one index down from that point (you get one doubled item ) and then that doubled item is replaced with the new one you are moving so the process it's actually longer than what you explained :)

Lyosha.
Автор

July 14, 2022 - Properly learned ArrayList and LinkedList. Thanks John!

gabenixon
Автор

It was like those lectures where the instructor teaches so smoothly so s/he puts everything in your mind without you noticing.

wickedsnuk
Автор

This is *by far* the easiest video to help understand this concept. As a relatively new programmer, I always found it somewhat puzzling to have different implementations of the List interface, but this video clears so many things up and gives actual reasons for their existence! Cheers!

alexanderrizzi
Автор

If someone is wondering why arrays have a constant time to get an element, it's because to get an element from the array, it makes a calculation, a really simple calculation actually.. the programs already knows the position that the array is located in the memory, and already knows the type of data the array is holding, so it can calculate the location of any index with a constant number of steps by doing : memoryPosition + (index * typeSize).

So, knowing the “start” position of the array, you just need to multiply the index by the amount of memory that this specific type takes. Let’s say you have an array that holds 100 int numbers, and let’s say the array is located at the space 1000 of the memory.. and int numbers take 4 bytes of memory each. So, to get the 50th element, we just need to multiply the index by the size of bytes (49 * 4) and we will get 196 bytes, now, just add the 1000 (the position that the array starts in the memory), you will get 1196 bytes, this is where the index number 49 is located. That's why it's constant, because you can have a 3 size array or a size array, the array will always do the same math calculation to get the index that you want to get.

dedz
Автор

I have problems understanding LinkedList despite reading numerous articles online. Your video is a god's gift!

lootster
Автор

ArrayList is still great if you are adding tons of stuff only to the end. It only needs to move stuff over when adding *not* at the end, otherwise it just places the element at the end and updates the current size. Additionally, it only needs to create a new array and copy everything over when the reserved capacity is exhausted. If you are keeping a reference to some node deep in the midst of the LinkedList, and adding or removing around *that*, then the LinkedList is faster. Also, if for some reason you are often adding and removing right at the beginning, a LinkedList comes into its own. Lastly, there is more memory overhead and less cache coherence with LinkedList. A funny quote I remember:
“Does anyone actually use LinkedList? I wrote it, and I never use it.” Joshua Bloch
Searching that gives some interesting information on it. As you said tho, for small data sizes, either of them would work great, you will likely never notice a difference unless your data gets larger.

jvsnyc
Автор

As someone who hasn't touched java except when an interview required it - watching your videos made me feel like I can code anything in java now. You're an excellent teach, bro. You've got a gift for sure.

ryuujisancodes
Автор

It’s so refreshing to hear an explanation that doesn’t have a heavy accent. Almost all my professors are hard to understand and it makes it difficult to learn

SpooxyCowboy
Автор

I learn java since 2014 but now I understand it. Huge thanks

amirulidzham
Автор

In most use cases, amortized analysis shows equivalence of run time.
Linked lists, however, lead to more cache misses (array can be bulk copied to cache with much fewer misses) which puts array in a huge advantage for practical reasons as well.

ginandi
Автор

Amazingly clear video, great job. Just a minor remark: To emphasize that the interface of the two lists is the same you could have used just List<String> as their type. Generally, that is the recommended way anyway.

slaki
Автор

Crazy how someone can explain all this clearly and simply in 10 minutes. Where my uni would take 2 mins of explaining nothing with a minimalistic slide showing what a linked list looks like. Thank you so much

DassVeryGood
Автор

Wish i had someone to teach me these stuff earlier…I had to learn these things the hard way. Awesome video man!!
Just one thing I think array list uses a load factor of (0.75) to decide when to scale up not when the list is totally full(e.g. like reached 10)

ayushgupta
Автор

How can a man be so precise with his teaching! Great job.

bekbolots
Автор

This is the most amazing course ever! Exactly what I want to know regarding of why use one from the other. Best examples and I got it rightaway. This guy is genius and should be a professor instead.

flytoinfinityvivi