What is the difference between ArrayList and LinkedList in Java?
| Point | ArrayList | LinkedList |
|---|---|---|
| Inside | Resizable array | Doubly linked nodes |
| Get by index | Fast, O(1) | Slow, O(n) |
| Add at the end | Fast (usually) | Fast |
| Add or remove at the start | Slow, O(n) | Fast, O(1) |
| Memory | Less | More (each node has two links) |
| Also a Deque | No | Yes |
In practice ArrayList is the better choice for almost all cases, because of its simple memory layout. For a queue or a stack use ArrayDeque. See Java HashSet and LinkedList.
Learn more about the similar topics:
| Tutorials |
|---|
| Java ArrayList |
| Java HashMap |
| Java HashSet and LinkedList |
| Java Generics |
| Exercises & Assignments |
|---|
| Sort a list of students with a Comparator |
| Count the frequency of words with a HashMap |
| Remove duplicates from a list and keep the order |