ArrayList and LinkedList are two popular implementations of the List interface in Java. Both store elements in insertion order and allow duplicate values, but they differ in their internal data structure and performance.
What is an ArrayList?
Let’s start with the structure. ArrayList internally uses a dynamic array to store its elements. This means it’s essentially a resizable array that can grow or shrink as needed. The elements are stored in contiguous memory locations.
Example for ArrayList :
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
System.out.println(arrayList); // Output: [A, B, C]
}
}
What is an LinkedList?
On the other hand, LinkedList internally uses a doubly linked list. Each element is stored in a node, and each node contains a reference to both the next and the previous element in the list. This allows easier insertion and removal of elements, but the elements are not stored in contiguous memory.
Example for LinkedList :
import java.util.LinkedList;
public class LinkedListStructureExample {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("A");
linkedList.add("B");
linkedList.add("C");
System.out.println(linkedList); // Output: [A, B, C]
}
}
Best 1–2 Line Answer
When modifying data, an ArrayList can be slower because removing an element requires shifting all the remaining elements to keep the array continuous.
LinkedList uses a doubly linked list structure, where each element is stored in a node that has references to both the next and previous nodes. This makes insertion and deletion easier, but the elements are not stored in continuous memory.

Top comments (0)