In this section we will learn what the LinkedList remove(), removeFirst(), and removeLast() methods are and how to use them in Java.
What is Java LinkedList remove() Method?
The Java LinkedList remove() method is used to remove an element from a LinkedList object, either its first (head) element or declaring specifically the element we want to remove as the argument of the method.
Java remove() Method Syntax:
public E remove() public E remove(int index) public boolean remove(Object o)
remove() Method Parameters:
For the first variant of the method, there’s no argument.
For the second variant: the method takes one argument and that is the index number where we want to remove the element there.
For the third variant of this method, it takes one argument and that is the element we want to remove from the list. Here then the method will search for the first occurrence of the specified element and only remove that first occurrence.
remove() Method Return Value:
For the first and second variants of the method, the return value is the removed element from the target LinkedList object.
For the third variant of the method, the return value of the method is of type boolean and will be true if the target element (the argument of the method) was removed from the list or false if the operation failed (like if the list didn’t have the element in the first place).
remove() Method Exceptions:
NoSuchElementException: we might get this exception for the first variant of this method if the target LinkedList object was in fact empty!
IndexOutOfBoundsException: For the second variant of this method, we might get this exception if the specified index was out of range in the target LinkedList object.
Example: using LinkedList remove() method
import java.util.LinkedList; class Main{ public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.push("John"); list.push("Omid"); list.push("Jack"); list.push("Ellen"); list.push("Elon"); list.remove("Omid"); list.remove(1); list.remove(); for (String s: list){ System.out.println(s); } } }
Output:
Jack John
What is Java LinkedList removeFirst() Method?
The Java LinkedList removeFirst() method is used to remove the first (head) element of a LinkedList object and return that as a result of calling the method.
Java removeFirst() Method Syntax:
public E removeFirst()
removeFirst() Method Parameters:
The method does not take an argument.
removeFirst() Method Return Value:
The return value of this method is the first (head) element of the list that was just removed as a result of calling the method.
removeFirst() Method Exceptions:
The method might throw one exception and that is:
NoSuchElementException: We get this exception if the target LinkedList is empty!
Example: using LinkedList removeFirst() method
import java.util.LinkedList; class Main{ public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.push("John"); list.push("Omid"); list.push("Jack"); list.push("Ellen"); list.push("Elon"); list.removeFirst(); list.removeFirst(); for (String s: list){ System.out.println(s); } } }
Output:
Jack Omid John
What is Java LinkedList removeLast() Method?
The Java LinkedList removeLast() method is used to remove the last (tail) element of a LinkedList object and return that as a result of calling the method.
Java removeLast() Method Syntax:
public E removeLast()
removeLast() Method Parameters:
The method does not take an argument.
removeLast() Method Return Value:
The return value of this method is the last element of the target LinkedList object that was removed as a result of calling the method.
removeLast() Method Exceptions:
The method might throw one exception and that is:
NoSuchElementException: We get this exception if the target LinkedList is empty!
Example: using LinkedList removeLast() method
import java.util.LinkedList; class Main{ public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.push("John"); list.push("Omid"); list.push("Jack"); list.push("Ellen"); list.push("Elon"); list.removeLast(); list.removeLast(); for (String s: list){ System.out.println(s); } } }
Output:
Elon Ellen Jack