Hereβs a complete and SEO-optimized article on:
Java LinkedList Methods β Full Guide with Syntax, Examples & Best Practices (2025)
Introduction β Why Use Java LinkedList Methods?
Javaβs LinkedList is a powerful data structure that combines the benefits of a list and a queue. It allows for fast insertions and deletions, especially at the beginning and middle of the list β and comes with a rich set of methods to make your code flexible and efficient.
By the end of this guide, youβll understand how to:
- Use all key
LinkedListmethods - Perform add, remove, access, and queue operations
- Know when to choose
LinkedListoverArrayList
What is a Java LinkedList?
LinkedList is a doubly-linked list implementation of the List, Deque, and Queue interfaces in Java. Unlike ArrayList, it:
- Allows fast insertions/removals from both ends
- Can be used as a stack, queue, or deque
- Is ideal for applications with frequent element shifting
Located in
java.utilpackage
import java.util.LinkedList;
LinkedList<String> names = new LinkedList<>();
Common Java LinkedList Methods
| Method | Purpose |
|---|---|
add() / add(index, element) | Adds element to end or at position |
addFirst() / addLast() | Adds element to beginning or end |
remove() / remove(index) / remove(object) | Removes first match or index |
removeFirst() / removeLast() | Removes from beginning or end |
get(index) | Gets element by index |
getFirst() / getLast() | Retrieves first/last element |
set(index, element) | Updates value at index |
contains() | Checks if element exists |
size() | Gets number of elements |
clear() | Empties the list |
peek(), poll(), pop(), offer() | Queue/stack-like operations |
indexOf() / lastIndexOf() | Gets index of element |
Java LinkedList Method Examples
1. add() and add(index, element)
LinkedList<String> list = new LinkedList<>();
list.add("Java");
list.add("Python");
list.add(1, "C++");
System.out.println(list);
Output:
[Java, C++, Python]
Adds elements at end or specified index.
2. addFirst() and addLast()
list.addFirst("Start");
list.addLast("End");
System.out.println(list);
Output:
[Start, Java, C++, Python, End]
Adds elements at both ends.
3. get() and getFirst() / getLast()
System.out.println(list.get(2)); // C++
System.out.println(list.getFirst()); // Start
System.out.println(list.getLast()); // End
Retrieves values from specified positions.
4. remove(), removeFirst(), removeLast()
list.remove("C++");
list.removeFirst();
list.removeLast();
System.out.println(list);
Output:
[Java, Python]
Removes elements by value or position.
5. set(index, value) β Update Value
list.set(1, "Go");
System.out.println(list);
Output:
[Java, Go]
Replaces element at given index.
6. contains() β Check for Element
System.out.println(list.contains("Java")); // true
Checks existence of a value.
7. size() and clear()
System.out.println(list.size()); // 2
list.clear();
System.out.println(list.isEmpty()); // true
Gets total count and clears list.
8. offer(), poll(), peek() β Queue Operations
LinkedList<String> queue = new LinkedList<>();
queue.offer("A");
queue.offer("B");
System.out.println(queue.poll()); // A
System.out.println(queue.peek()); // B
Queue-like behavior:
offer()= enqueuepoll()= dequeuepeek()= view front
9. push() and pop() β Stack Behavior
LinkedList<String> stack = new LinkedList<>();
stack.push("First");
stack.push("Second");
System.out.println(stack.pop()); // Second
Stack-like LIFO (last-in, first-out) operations.
Summary Table of Methods
| Operation | Methods |
|---|---|
| Add | add(), addFirst(), addLast(), offer() |
| Remove | remove(), removeFirst(), removeLast(), poll(), pop() |
| Access | get(), getFirst(), getLast(), peek() |
| Update | set(index, value) |
| Check | contains(), isEmpty() |
| Size & Clear | size(), clear() |
| Search Index | indexOf(), lastIndexOf() |
| Stack / Queue | push(), pop(), offer(), poll() |
Tips for Using LinkedList Methods
- Use
addFirst()/removeFirst()for queue operations. - Use
push()/pop()for stack operations. - Avoid
get(index)if random access is frequent β preferArrayListfor that. -
LinkedListhas higher memory usage thanArrayList.
Summary β Java LinkedList Methods
Java LinkedList offers:
- Dual support for List and Queue
- Efficient add/remove from front or end
- Built-in methods for search, queue, stack, and iteration
Mastering these methods enables you to write flexible, performant code for any real-world application.
FAQs on Java LinkedList Methods
Is LinkedList ordered?
Yes. LinkedList maintains insertion order.
Can LinkedList contain duplicates?
Yes, duplicates are allowed.
Whatβs the difference between LinkedList and ArrayList?
| Feature | ArrayList | LinkedList |
|---|---|---|
| Access Speed | Fast (O(1)) | Slow (O(n)) |
| Insertion/Deletion | Slow (shifts) | Fast (no shift) |
| Memory Usage | Lower | Higher |
Is LinkedList synchronized?
No. Use Collections.synchronizedList(new LinkedList<>()) for thread safety.
How to convert LinkedList to Array?
String[] arr = list.toArray(new String[0]);
Share Now :
