Learn Java ArrayList and LinkedList, their differences, methods, and how to use these List implementations to store, access, and manipulate dynamic collections efficiently.
ArrayList and LinkedList in Java – Complete Detailed Tutorial
In Java, List interface is implemented by ArrayList and LinkedList. Both store ordered collections and allow duplicate elements, but differ in internal implementation and performance.
1. ArrayList
- Resizable array implementation of List
- Dynamic size, unlike arrays
- Allows duplicates, maintains insertion order
- Provides fast random access (
get(index)) - Slower insertions/deletions in the middle
Common Methods:
| MethodDescription | |
| add(E e) | Adds element at end |
| add(int index, E e) | Adds element at specified index |
| get(int index) | Returns element at index |
| remove(int index) | Removes element at index |
| size() | Returns number of elements |
| contains(Object o) | Checks if element exists |
Example – Using ArrayList
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // duplicate allowed
System.out.println("ArrayList: " + fruits);
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
System.out.println("Element at index 1: " + fruits.get(1));
}
}
Output:
ArrayList: [Apple, Banana, Orange, Apple]
After removal: [Apple, Orange, Apple]
Element at index 1: Orange
2. LinkedList
- Doubly linked list implementation of List and Deque
- Maintains insertion order, allows duplicates
- Faster insertions/deletions in the middle than ArrayList
- Slower random access (
get(index))
Common Methods:
| MethodDescription | |
| add(E e) | Adds element at end |
| addFirst(E e) | Adds element at beginning |
| addLast(E e) | Adds element at end |
| get(int index) | Returns element at index |
| removeFirst() | Removes first element |
| removeLast() | Removes last element |
| size() | Returns number of elements |
Example – Using LinkedList
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // duplicate allowed
System.out.println("LinkedList: " + fruits);
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
System.out.println("First element: " + ((LinkedList<String>) fruits).getFirst());
System.out.println("Last element: " + ((LinkedList<String>) fruits).getLast());
}
}
Output:
LinkedList: [Apple, Banana, Orange, Apple]
After removal: [Apple, Orange, Apple]
First element: Apple
Last element: Apple
3. ArrayList vs LinkedList
| FeatureArrayListLinkedList | ||
| Implementation | Resizable array | Doubly linked list |
| Access time | Fast random access (get(index)) | Slow random access |
| Insert/Delete | Slow (middle elements) | Fast (middle elements) |
| Memory | Less overhead | More memory (pointers) |
| Use case | Frequent access | Frequent insertion/deletion |
4. Key Points
- ArrayList: best for frequent access, less insertion/deletion
- LinkedList: best for frequent insertion/deletion, sequential access
- Both implement List interface, maintain insertion order, and allow duplicates
- Can use Iterator to traverse both
5. Summary
- ArrayList: dynamic array, fast access, slower insert/delete
- LinkedList: doubly linked list, fast insert/delete, slower access
- Both are part of Java Collections Framework and widely used in applications