Java ArrayList and LinkedList – Complete Guide with Examples
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
Output:
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
Output:
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