In this section we will learn what the Deque remove(), removeFirst(), and removeLast() methods are and how to use them in Java.
What is Java Deque remove() Method?
The Java Deque remove() method is used to remove the first (head) element of a Deque object and return that as a result.
Java remove() Method Syntax:
E remove();
remove() Method Parameters:
The method does not take an argument.
remove() Method Return Value:
The method returns the first (head) element of the target Deque object.
Note: if the Deque was empty, an exception will be thrown instead.
remove() Method Exceptions:
The method might throw one exception:
NoSuchElementException: we get this exception if the target Deque object is empty.
Example: using Deque remove() method
import java.util.ArrayDeque; import java.util.Deque; class Main{ public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<>(); deque.add(30); deque.add(20); deque.add(10); deque.add(40); deque.add(50); System.out.println("The size of the Deque object before calling the remove method: "+deque.size()); System.out.println("Calling the method and here's the value it returned: "+deque.remove()); System.out.println("The size of the Deque object after calling the remove method: "+deque.size()); } }
Output:
The size of the Deque object before calling the remove method: 5 Calling the method and here's the value it returned: 30 The size of the Deque object after calling the remove method: 4
What is Java Deque removeFirst() Method?
The Java Deque removeFirst() method is used to remove the first element of a Deque object and return that as a result.
Java removeFirst() Method Syntax:
E removeFirst()
removeFirst() Method Parameters:
The method does not take an argument.
removeFirst() Method Return Value:
The method returns the first (head) element of the target Deque object.
Note: if the Deque was empty, an exception will be thrown instead.
removeFirst() Method Exceptions:
The method might throw one exception:
NoSuchElementException: we get this exception if the target Deque object is empty.
Example: using Deque removeFirst() method
import java.util.ArrayDeque; import java.util.Deque; class Main{ public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<>(); deque.add(30); deque.add(20); deque.add(10); deque.add(40); deque.add(50); System.out.println("The size of the Deque object before calling the removeFirst method: "+deque.size()); System.out.println("Calling the method and here's the value it returned: "+deque.removeFirst()); System.out.println("The size of the Deque object after calling the removeFirst method: "+deque.size()); } }
Output:
The size of the Deque object before calling the removeFirst method: 5 Calling the method and here's the value it returned: 30 The size of the Deque object after calling the removeFirst method: 4
What is Java Deque removeLast() Method?
The Java Deque removeLast() method is used to remove the last (tail) element of a Deque object.
This removed value will be returned as a result of calling the method.
Java removeLast() Method Syntax:
E removeLast()
removeLast() Method Parameters:
The method does not take an argument.
removeLast() Method Return Value:
The method returns the last (tail) element of the target Deque object.
Note: if the Deque was empty, an exception will be thrown instead.
removeLast() Method Exceptions:
The method might throw one exception:
NoSuchElementException: we get this exception if the target Deque object is empty.
Example: using Deque removeLast() method
import java.util.ArrayDeque; import java.util.Deque; class Main{ public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<>(); deque.add(30); deque.add(20); deque.add(10); deque.add(40); deque.add(50); System.out.println("The size of the Deque object before calling the removeLast method: "+deque.size()); System.out.println("Calling the method and here's the value it returned: "+deque.removeLast()); System.out.println("The size of the Deque object after calling the removeLast method: "+deque.size()); } }
Output:
The size of the Deque object before calling the removeLast method: 5 Calling the method and here's the value it returned: 50 The size of the Deque object after calling the removeLast method: 4