r/learnjava • u/maybe_tom__ • Dec 21 '20
ArrayList vs LinkedList
Hi everyone, i want to solve a thought about ArrayList vs LinkedList. I know that ArrayList are basically an resizable array that start from a initial size of 10 (if not otherwise specified in constructor) that is resized everytime you try to put something over its actual length, and LinkedList have items that have pointers to next and previous Object. So linked list are more efficient with removing, adding and set items in the list, cause you don't have to resize all the array or reorder elements but you only change pointers... so the point is -> Why i would choose to use an ArrayList instead LinkedList?
9
u/JasburyCS Dec 21 '20
The general rule of thumb: ArrayLists will be better any time you won’t be doing a lot of elements shifting and any time you need to randomly access any specific index efficiently (using .get() in Java if I remember correctly). This snowballs fast because many sorting algorithms, for example, rely on indexing. Most problems fall under this category.
Conversely, Linked lists are good at being dynamic if you plan on doing many operations that would cause an entire array to be shifted. This could include adding to the middle, removing from the middle, or adding/removing from the front of the list.
In general, yes an array list will solve most problems. Linked lists have more niche use cases. And in practice a lot of it will come to what your data looks like. Sure, copying array elements is fast in Java. But what what happens if the amount of data you have to copy to shift elements over becomes huge? And these shifts happen over and over again in your code? That’s when linked lists start to look more appealing
3
2
1
u/HecknChonker Dec 21 '20
Also check out ArrayDeque. It's a double ended queue that can be used as a stack or queue or list. ArrayList is still better in most cases, but this is a good one to be familiar with.
1
Dec 21 '20
LinkedList has additional node object that keeps reference to value object, so it's unnecessary overhead.
Whenever you would need a linked list in your code, intrusive linked list is better for performance/memory than using a wrapper.
1
1
u/aryehof Dec 23 '20
Use an ArrayList when the overhead of resizing is inconsequential. There are cases (particularly in control system software) where that resizing is consequential and therefore a linked list is preferred.
-1
u/aarjay_m Dec 21 '20
From the implementation perspective, they are similar. One major difference is the performance. ArrayList is NOT threadsafe and therefore faster. LinkedList is threadsafe, therefore slower in comparison to ArrrayList.
(I’ll assume you understand the concepts around “threadsafe”. If not, feel free to ask follow up questions).
1
u/maybe_tom__ Dec 22 '20
I know ty, i'm not so new to Java, i had this thought and i wanted to discuss it here
1
1
u/deltageek Dec 22 '20
Even if any of the basic list implementations are threadsafe, you’d be better off using one of the concurrent collections in java.util.concurrent that were specifically designed for concurrency
61
u/feral_claire Dec 21 '20
In practice, pretty much always chose ArrayList over LinkedList. It's usually the better choice.
ArrayList is better at appending to and removing at the end, iteration, and random access. It also uses way less memory (LinkedList uses about 5 times as much).
The only think LinkedList is better is, is adding and removing from the front of the list. It's only better at adding or removing in the middle of the list if you do it while already iterating the list, since you need to loop through the list to get to a specific index, it's still faster with ArrayList if you want to insert a random index even though it means copying the list to make room.
The reason adding and removing at the end is so much faster is because you are just setting a value in the array versus needing to create a whole node object and set a pointer to it. Yes you need to resize the array sometimes but since the array in ArrayList is oversized you rarely need to do it doesn't make a big impact. Copying arrays is also very fast.