ArrayList and LinkedList are two commonly used classes in Java that implement the List interface. Both maintain insertion order and allow duplicate elements, but they differ in memory structure, data access, and performance for insertion and deletion operations.
- ArrayList is based on a dynamic array, while LinkedList uses a doubly linked list
- ArrayList provides faster random access, whereas LinkedList is efficient for frequent insertions and deletions
- Both are part of the Java Collections Framework and support generic data storage
ArrayList
ArrayList is a class in Java Collections Framework that stores elements in a sequential manner and automatically grows in size when needed. It is widely used for fast data retrieval and managing ordered collections of elements.
- Allows storing elements of the same or different data types using generics
- Supports index-based operations like searching, updating, and accessing elements
- Maintains insertion order of elements during storage and retrieval
Syntax:
ArrayList<DataType> listName = new ArrayList<>();
import java.util.ArrayList;
public class GFG{
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println(list);
System.out.println("Element at index 1: " + list.get(1));
}
}
Output
[Java, Python, C++] Element at index 1: Python
Explanation:
- Elements are stored in contiguous memory locations
- Accessing elements using index is fast
- When the internal array becomes full, a new larger array is created and elements are copied
LinkedList
LinkedList is a class in Java Collections Framework that stores elements as interconnected nodes instead of a continuous memory structure. It is mainly used when frequent insertion and deletion operations are required.
- Implements both List and Deque interfaces in Java
- Allows efficient insertion and deletion of elements from both ends
- Maintains insertion order and supports duplicate elements
Syntax:
LinkedList<DataType> listName = new LinkedList<>();
import java.util.LinkedList;
public class GFG{
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println(list);
System.out.println("First element: " + list.getFirst());
}
}
Output
[Java, Python, C++] First element: Java
Explanation:
- Each element is stored as a node. Every node contains:
- Data
- Reference to previous node
- Reference to next node
- No shifting of elements during insertion or deletion
ArrayList vs LinkedList
| Feature | ArrayList | LinkedList |
|---|---|---|
| Definition | A resizable array implementation of the List interface | A doubly-linked list implementation of the List interface |
| Data Storage | Stores elements in contiguous memory locations | Stores elements as nodes linked using pointers |
| Access Time | Fast random access using index (O(1)) | Slower random access (O(n)) |
| Insertion/Deletion | Slower in middle or beginning (O(n)), faster at end | Faster for insertion/deletion anywhere (O(1) if position known) |
| Memory Usage | Less memory overhead | More memory due to storing pointers |
| Iteration | Faster iteration using index-based loop | Faster iteration using iterator |
| Use Case | Best for frequent access and rare modifications | Best for frequent insertions and deletions |