Java LinkedList
A Java LinkedList is an ordered collection from the java.util package. It can work like a normal list, but it also has helpful methods for adding and removing items at the beginning or end.
Use a LinkedList when your program often changes the front or back of a list, or when you want one object that can act like a list, queue, or deque.
Create A LinkedList
Import java.util.LinkedList, then choose the object type the list will store. Like ArrayList, a LinkedList can store duplicates, keeps insertion order, and uses zero-based indexes.
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cities = new LinkedList<>();
cities.add("Paris");
cities.add("Tokyo");
cities.add("Lima");
System.out.println(cities);
System.out.println("First: " + cities.get(0));
System.out.println("Size: " + cities.size());
}
}
Output:
[Paris, Tokyo, Lima]
First: Paris
Size: 3
The add() method adds an item to the end of the list. The get() method reads an item by index, and size() returns the number of items.
Add And Remove From Both Ends
A LinkedList has special methods for the front and back of the list. Use addFirst() and addLast() to add items. Use removeFirst() and removeLast() to remove items.
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> steps = new LinkedList<>();
steps.add("Install Java");
steps.add("Write code");
steps.addFirst("Open editor");
steps.addLast("Run program");
System.out.println(steps);
System.out.println("Removed: " + steps.removeFirst());
System.out.println("Last step: " + steps.getLast());
System.out.println(steps);
}
}
Output:
[Open editor, Install Java, Write code, Run program]
Removed: Open editor
Last step: Run program
[Install Java, Write code, Run program]
After addFirst(), the new item becomes the first element. After removeFirst(), that first element is removed and returned.
Use LinkedList As A Queue
A queue processes items in first-in, first-out order. LinkedList implements the Queue interface, so you can store it in a Queue variable and use queue methods such as offer(), peek(), and poll().
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> printJobs = new LinkedList<>();
printJobs.offer("Report.pdf");
printJobs.offer("Schedule.txt");
printJobs.offer("Invoice.docx");
System.out.println("Next: " + printJobs.peek());
System.out.println("Printed: " + printJobs.poll());
System.out.println("Next: " + printJobs.peek());
System.out.println(printJobs);
}
}
Output:
Next: Report.pdf
Printed: Report.pdf
Next: Schedule.txt
[Schedule.txt, Invoice.docx]
The peek() method reads the next item without removing it. The poll() method removes and returns the next item.
LinkedList Versus ArrayList
| Feature | ArrayList |
LinkedList |
|---|---|---|
| Common use | Indexed list access | Adding or removing at the ends |
| Access by index | Usually faster | Usually slower for large lists |
| First and last methods | No special list methods | Has addFirst(), getLast(), and more |
| Queue behavior | Not a queue implementation | Implements Queue and Deque |
For many beginner programs, ArrayList is the default list choice because index access is common. Choose LinkedList when the first and last operations or queue behavior match the problem better.
Common LinkedList Methods
add(value)adds a value to the end.addFirst(value)adds a value to the beginning.addLast(value)adds a value to the end.getFirst()reads the first value.getLast()reads the last value.removeFirst()removes and returns the first value.removeLast()removes and returns the last value.size()returns the number of values.
Common Mistakes
- Forgetting
import java.util.LinkedList;. - Using
lengthinstead ofsize(). - Calling
getFirst()orremoveFirst()on an empty list. - Choosing
LinkedListonly because the name sounds advanced, even whenArrayListwould be simpler.
Takeaway: LinkedList is an ordered collection that is most useful when you need list operations plus efficient work at the front or back.
