๐ Java LinkedList โ Complete Guide with Syntax, Examples & Use Cases
๐งฒ Introduction โ Why Java LinkedList Is Important
Imagine you’re managing a dynamic playlist or task queue where items are frequently added or removed โ an ArrayList would slow down due to shifting. This is where Java LinkedList
shines.
The LinkedList
class is part of Javaโs Collections Framework, offering a doubly-linked list structure for efficient insertion and deletion.
By the end of this article, youโll learn:
โ
How Java LinkedList
works and when to use it
โ
Syntax, core methods, and real-world examples
โ
Differences between LinkedList
and ArrayList
โ
Best practices and performance insights
๐ What Is LinkedList in Java?
๐
LinkedList
in Java is a doubly-linked list implementation of theList
,Deque
, andQueue
interfaces.
It allows:
- Dynamic resizing
- Fast insertion/deletion at both ends
- Support for null, duplicates, and sequential access
๐งช Basic Syntax โ Creating a LinkedList
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> colors = new LinkedList<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
System.out.println(colors); // Output: [Red, Green, Blue]
}
}
โ
LinkedList<String>
declares a list of Strings
โ
add()
appends elements to the end
โ
Insertion order is preserved
๐ฆ LinkedList Key Methods
Method | Description | Example |
---|---|---|
add(E e) | Add element to end | list.add("Orange") |
addFirst(E e) | Insert at the beginning | list.addFirst("Mango") |
addLast(E e) | Insert at the end | list.addLast("Peach") |
getFirst() | Get first element | list.getFirst() |
getLast() | Get last element | list.getLast() |
removeFirst() | Remove from front | list.removeFirst() |
removeLast() | Remove from end | list.removeLast() |
peek() | Retrieve head without removing | list.peek() |
poll() | Retrieve and remove head | list.poll() |
๐ Iterating Over a LinkedList
๐งฉ Using for-each loop
for (String color : colors) {
System.out.println(color);
}
๐งฉ Using forEach() (Java 8+)
colors.forEach(System.out::println);
๐ LinkedList as Queue and Stack
๐ฅ Queue Behavior (FIFO)
LinkedList<String> queue = new LinkedList<>();
queue.add("Task 1");
queue.add("Task 2");
System.out.println(queue.poll()); // Task 1
๐ค Stack Behavior (LIFO)
LinkedList<Integer> stack = new LinkedList<>();
stack.push(10);
stack.push(20);
System.out.println(stack.pop()); // 20
โ
push()
/pop()
make LinkedList behave like a stack
โ
add()
/poll()
behave like a queue
๐ LinkedList vs ArrayList
Feature | LinkedList | ArrayList |
---|---|---|
Underlying Structure | Doubly linked list | Dynamic array |
Access time | โ Slower (O(n)) | โ Fast (O(1) for index) |
Insert/Delete | โ Fast at start/mid (O(1)/O(n)) | โ Slower (due to shifting) |
Memory usage | More (extra node pointers) | Less |
Use case | Frequent insert/delete | Frequent read/access |
๐งฐ Real-World Example โ Task Scheduler
LinkedList<String> tasks = new LinkedList<>();
tasks.add("Design UI");
tasks.add("Write API");
tasks.addFirst("Login");
System.out.println("First task: " + tasks.getFirst());
โ
Add high-priority tasks at the front
โ
Useful for job queues, undo stacks, playlists, etc.
๐ก Best Practices for Using LinkedList
โ
Use when insertions/deletions are frequent
โ
Avoid for random access-heavy operations
โ
Use Deque
interface methods (addFirst
, removeLast
) for stack/queue behavior
โ
Avoid mixing ArrayList
and LinkedList
unless necessary
โ Summary
LinkedList
is a versatile, doubly-linked structure in Java- Ideal for frequent insert/delete operations
- Can function as List, Queue, or Stack
- Offers built-in methods for both end-based and index-based manipulation
โ FAQs โ Java LinkedList
โ Can LinkedList store null
values?
Yes. It can store multiple null
elements.
โ Is LinkedList synchronized?
No. Use Collections.synchronizedList()
or ConcurrentLinkedDeque
for thread safety.
โ How does LinkedList differ from ArrayList?
LinkedList
offers faster insert/delete, but slower random access.
โ Can LinkedList be used as a Queue?
Yes. It implements the Queue
and Deque
interfaces.
โ What is the time complexity of adding/removing elements?
- Add/remove at ends: O(1)
- Add/remove in middle: O(n)
- Access by index: O(n)
Share Now :