Java LinkedList Methods

About This Index
This page is the Java LinkedList Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, and all of them compile on Java 8 and later.
New to LinkedList? It is a doubly-linked list from the java.util package, so you write import java.util.LinkedList; first. Every element sits in its own node that points to the node before and the node after it. That makes adding or removing at the front or back very fast, while get(i) has to walk node by node. LinkedList is both a List and a Deque, so you can use it as a list with add() and remove(), or as a queue or stack with poll() and push(). It allows duplicates and null.
Methods like addFirst(), getLast(), poll(), and pop() come from Deque and Queue, so declare your variable as LinkedList to use them: LinkedList<String> names = new LinkedList<>();. A plain List variable only sees the list methods. Also remember that on a LinkedList<Integer>, remove(1) removes the element at index 1, not the number 1.
How to Call a LinkedList Method
Create a LinkedList, then call each method on the variable:
import java.util.LinkedList;
LinkedList<String> queue = new LinkedList<>();
queue.add("Ana"); // append: [Ana]
queue.addFirst("Zed"); // front: [Zed, Ana]
queue.addLast("Bo"); // back: [Zed, Ana, Bo]
String head = queue.getFirst(); // "Zed", still in the list
String next = queue.poll(); // removes and returns "Zed"
int count = queue.size(); // 2 Java LinkedList Methods Table
All method guides in this Java tutorial series (44 tutorials), grouped by purpose.
Add elements
8 methodsPut a new element at the end, at the front, or at an index, one at a time or a whole collection at once. Includes the queue-style offer methods and the stack-style push.
| Method | Description | Tutorial |
|---|---|---|
add() | Append an element to the end, or insert it at an index. | Try it |
addAll() | Append every element of another collection, or insert them at an index. | Try it |
addFirst() | Insert an element at the front (head) of the list. | Try it |
addLast() | Insert an element at the end (tail) of the list. | Try it |
offer() | Add an element to the tail of the queue and return true. | Try it |
offerFirst() | Insert an element at the front and return true. | Try it |
offerLast() | Insert an element at the tail and return true. | Try it |
push() | Push an element onto the front of the list, like a stack. | Try it |
Read without removing
7 methodsLook at an element by index, or at the first or last one, without taking it out of the list. Some methods throw on an empty list and some return null.
| Method | Description | Tutorial |
|---|---|---|
get() | Read the element at a given index. | Try it |
getFirst() | Read the first element, or throw if the list is empty. | Try it |
getLast() | Read the last element, or throw if the list is empty. | Try it |
element() | Read the head of the queue, or throw if it is empty. | Try it |
peek() | Look at the head without removing it; null if empty. | Try it |
peekFirst() | Look at the first element without removing it; null if empty. | Try it |
peekLast() | Look at the last element without removing it; null if empty. | Try it |
Remove one element
9 methodsTake out the first, the last, or a chosen element, by index or by value. The poll methods return null on an empty list, the others throw.
| Method | Description | Tutorial |
|---|---|---|
remove() | Remove the head, the element at an index, or the first match. | Try it |
removeFirst() | Remove and return the first element, or throw if empty. | Try it |
removeLast() | Remove and return the last element, or throw if empty. | Try it |
poll() | Remove and return the head, or null if the list is empty. | Try it |
pollFirst() | Remove and return the first element, or null if empty. | Try it |
pollLast() | Remove and return the last element, or null if empty. | Try it |
pop() | Pop the top element of the stack, or throw if it is empty. | Try it |
removeFirstOccurrence() | Remove the first element equal to a value, searching from the head. | Try it |
removeLastOccurrence() | Remove the last element equal to a value, searching from the tail. | Try it |
Remove many and clear
4 methodsRemove every element that matches a collection or a condition, keep only the common ones, or empty the whole list.
| Method | Description | Tutorial |
|---|---|---|
removeAll() | Remove every element that also appears in another collection. | Try it |
retainAll() | Keep only the elements that also appear in another collection. | Try it |
removeIf() | Remove every element that passes a test, using a lambda. | Try it |
clear() | Remove every element, leaving an empty list. | Try it |
Change, search, and size
8 methodsReplace elements, transform or sort the whole list, find where a value sits, check that it is there, and ask how many elements there are.
| Method | Description | Tutorial |
|---|---|---|
set() | Replace the element at an index and return the old one. | Try it |
replaceAll() | Replace every element with the result of a function. | Try it |
sort() | Sort the list in place, using natural order or a Comparator. | Try it |
indexOf() | Find the index of the first matching element, or -1. | Try it |
lastIndexOf() | Find the index of the last matching element, or -1. | Try it |
contains() | Check whether the list holds a given element. | Try it |
size() | Return the number of elements in the list. | Try it |
isEmpty() | Check whether the list has no elements. | Try it |
Loop, view, and copy
8 methodsWalk through the list with iterators, forEach, or a spliterator, look at a slice with subList, and copy the elements to an array or to a new list.
| Method | Description | Tutorial |
|---|---|---|
iterator() | Walk the list from the first element to the last, one at a time. | Try it |
listIterator() | Get an iterator that moves both ways and can add or replace elements. | Try it |
descendingIterator() | Walk the list from the last element to the first. | Try it |
forEach() | Run a piece of code for every element, using a lambda. | Try it |
spliterator() | Get a Spliterator, the traversal tool used by Java streams. | Try it |
subList() | Get a live view of a range of the list. | Try it |
toArray() | Copy the elements of the list into an array. | Try it |
clone() | Make a shallow copy of the list. | Try it |
Frequently Asked Questions
Start with add()
Append an element to the end or insert it at an index, the first guide in the list.
12 people found this page helpful
