This example shows how to remove elements from the LinkedList in Java. This example also shows various methods to remove elements including remove, removeAll, and clear methods.
How to remove elements from LinkedList in Java?
There are several ways using which you can remove elements from the LinkedList as given below.
1. Remove an element using the remove method
To remove an element from the LinkedList object, use the remove
method and pass the index of the element.
1 |
public E remove(int index) |
This method removes an element located at the specified index of the linked list object and returns the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.LinkedList; public class LinkedListRemoveElementsExample { public static void main(String[] args) { //create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); /* * To remove an element at the specified index, use * the remove method */ String element = linkedListColors.remove(1); System.out.println(element + " is removed from the list"); System.out.println("LinkedList contains: " + linkedListColors); } } |
Output
1 2 |
Green is removed from the list LinkedList contains: [Red, Blue] |
Note: If the specified index is out of the range, i.e. index is less than 0 or greater than or equal to the list size, the remove
method throws IndexOutOfBoundsException exception.
There is an overloaded remove
method that accepts the Object instead of the index.
1 |
public boolean remove(Object o) |
This method removes the specified element from the list. It returns true if the element is found and removed from the list, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); //this will remove "Green" and return true System.out.println( linkedListColors.remove("Green") ); System.out.println("LinkedList contains: " + linkedListColors); //this will return false as "Yellow" is not present in the list System.out.println( linkedListColors.remove("Yellow") ); System.out.println("LinkedList contains: " + linkedListColors); |
Output
1 2 3 4 |
true LinkedList contains: [Red, Blue] false LinkedList contains: [Red, Blue] |
Note: If the linked list contains objects of a custom class, the class must implement the equals
and hashCode
methods for these methods to work.
2. Removing the first and last elements
The removeFirst
and removeLast
methods delete the first and last element of the linked list respectively. Both of the methods return the element that was removed from the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
//create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); /* * To remove first element, use the * removeFirst method */ String firstElement = linkedListColors.removeFirst(); System.out.println(firstElement + " is removed from the list"); System.out.println("LinkedList contains: " + linkedListColors); /* * To remove last element, use the * removeLast method */ String lastElement = linkedListColors.removeLast(); System.out.println(lastElement + " is removed from the list"); System.out.println("LinkedList contains: " + linkedListColors); |
Output
1 2 3 4 |
Red is removed from the list LinkedList contains: [Green, Blue] Blue is removed from the list LinkedList contains: [Green] |
Since the LinkedList class is a linked list implementation in Java, it also supports poll operations. You can use the pollFirst
and pollLast
methods instead of the removeFirst
and removeLast
methods to remove first and last elements respectively.
You can also use the remove
method without any arguments to remove the first or head element of the LinkedList object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//create new LinkedList object LinkedList<String> linkedListColors = new LinkedList<String>(); //add elements linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); /* * To remove first element, use the * remove method */ String firstElement = linkedListColors.remove(); System.out.println(firstElement + " is removed from the list"); System.out.println("LinkedList contains: " + linkedListColors); |
Output
1 2 |
Red is removed from the list LinkedList contains: [Green, Blue] |
3. Remove the first and the last occurrence of an element
The removeFirstOccurrence
method of the LinkedList class removes the first occurrence of the element in the list.
1 |
public boolean removeFirstOccurrence(Object o) |
Similarly, the removeLastOccurrence
method removes the last occurrence of the element in the list.
1 |
public boolean removeLastOccurrence(Object o) |
Both of these methods return true if the element is found and is removed in the linked list, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//create new LinkedList object LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); //add elements linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); /* * To remove first occurrence of the element, use * the removeFirstOccurrence method */ System.out.println( linkedListNumbers.removeFirstOccurrence(1) ); System.out.println("LinkedList contains: " + linkedListNumbers); /* * To remove last occurrence of the element, use * the removeLastOccurrence method */ System.out.println( linkedListNumbers.removeLastOccurrence(3) ); System.out.println("LinkedList contains: " + linkedListNumbers); //this will return false, as the list does not contain 4 System.out.println( linkedListNumbers.removeFirstOccurrence(4) ); |
Output
1 2 3 4 5 |
true LinkedList contains: [2, 3, 1, 2, 3] true LinkedList contains: [2, 3, 1, 2] false |
Note: If the linked list contains objects of a custom class, the class must implement the equals
and hashCode
methods for these methods to work.
4. Removing all elements
The clear
method of the LinkedList class removes all elements from the LinkedList.
1 2 3 4 5 6 7 8 9 10 11 |
//create new LinkedList object LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); //add elements linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); linkedListNumbers.clear(); System.out.println("LinkedList contains: " + linkedListNumbers); |
Output
1 |
LinkedList contains: [] |
Visit how to clear LinkedList example to know more.
This example is a part of the LinkedList in Java tutorial.
References:
Java 8 LinkedList
Please let me know your views in the comments section below.