The Wayback Machine - https://web.archive.org/web/20240618020256/https://www.geeksforgeeks.org/arraydeque-removelast-method-in-java/
Open In App

ArrayDeque removeLast() Method in Java

Last Updated : 10 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The Java.util.ArrayDeque.removeLast() method is used to remove the last element of the Deque.

Syntax:

Array_Deque.removeLast()

Parameters: The method does not take any parameters.

Return Value: This method returns the last element of the Deque after removing it.

Exceptions: The method throws NoSuchElementException if the deque is empty.

Below programs illustrate the Java.util.ArrayDeque.removeLast() method:

Program 1:




// Java code to illustrate removeLast()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        Deque<String> de_que = new ArrayDeque<String>();
  
        // Use add() method to add elements into the Deque
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("For");
        de_que.add("Geeks");
  
        // Displaying the ArrayDeque
        System.out.println("Initial ArrayDeque: " + de_que);
  
        // Removing elements using removeLast() method
        de_que.removeLast();
        de_que.removeLast();
        de_que.removeLast();
  
        // Displaying the ArrayDeque after removal
        System.out.println("ArrayDeque after removing "
                           + "elements: " + de_que);
    }
}


Output:

Initial ArrayDeque: [Welcome, To, Geeks, For, Geeks]
ArrayDeque after removing elements: [Welcome, To]

Program 2:




// Java code to illustrate removeLast()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        Deque<Integer> de_que = new ArrayDeque<Integer>();
  
        // Use add() method to add elements into the Deque
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
  
        // Displaying the ArrayDeque
        System.out.println("Initial ArrayDeque: " + de_que);
  
        // Removing elements using removeLast() method
        de_que.removeLast();
        de_que.removeLast();
  
        // Displaying the ArrayDeque after removal
        System.out.println("ArrayDeque after removing "
                           + "elements: " + de_que);
    }
}


Output:

Initial ArrayDeque: [10, 15, 30, 20, 5]
ArrayDeque after removing elements: [10, 15, 30]


Previous Article
Next Article

Similar Reads

LinkedList removeLast() Method in Java
In Java, the LinkedList class provides the removeLast() method to remove and return the last element of the list. If the list is empty, the method throws a NoSuchElementException. Example: Java Code import java.util.LinkedList; public class Example { public static void main(String[] args) { LinkedList&lt;String&gt; list = new LinkedList&lt;&gt;();
2 min read
LinkedBlockingDeque removeLast() method in Java
The removeLast() method of LinkedBlockingDeque returns and removes the element at tail of the Deque container. The method throws an NoSuchElementException if the Deque container is empty. Syntax: public E removeLast() Parameters: This method does not accepts any parameter. Returns: This method returns the tail of the Deque container. Exception: The
2 min read
ConcurrentLinkedDeque removeLast() method in Java
The ConcurrentLinkedDeque.removeLast() is an in-built function in Java which removes the last element in the deque. The function throws a NoSuchElementException if this deque is empty. Syntax: Conn_Linked_Deque.removeLast() Parameters: The function does not accepts any parameter. Return Values: The function returns the last element in the deque. Ex
2 min read
Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Java.util.ArrayDeque Class in Java | Set 1
java.util.ArrayDeque class describes an implementation of a resizable array structure which implements the Deque interface. Array deques has not immutable and can grow as necessary. They are not thread-safe and hence concurrent access by multiple threads is not supported unless explicitly synchronized. Null elements are invalid in this structure. M
5 min read
Java.util.ArrayDeque Class in Java | Set 2
Java.util.ArrayDeque Class in Java | Set 1 More Methods of util.ArrayDeque Class : 16. offer(Element e) : java.util.ArrayDeque.offer(Element e) : inserts element at the end of deque. Syntax : public boolean offer(Element e) Parameters : e - element to add Return : true, if element is added else; false 17. offerFirst(Element e) : java.util.ArrayDequ
5 min read
ArrayDeque offerLast() Method in Java
The Java.util.ArrayDeque.offerLast(Object element) method in Java is used to add a specific element at the end of this Deque. The function is similar to the addLast(), add() and offer() method of ArrayDeque in Java. Syntax: Array_Deque.offerLast(Object element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to
2 min read
ArrayDeque toArray() Method in Java
The java.util.ArrayDeque.toArray() method is used to form an array of the same elements as that of the Deque. Basically, the method copies all the element from this deque to a new array. Syntax: Object[] arr = Array_Deque.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the elements
2 min read
ArrayDeque push() Method in Java
The Java.util.ArrayDeque.push(E element) method is used to push an element into the Deque. The operation is similar to the operation in the stack. The element gets pushed onto the top of the deque. Syntax: Array_Deque.push(E element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to be pushed into the deque. R
2 min read
ArrayDeque pop() Method in Java
The Java.util.ArrayDeque.pop() method in Java is used to pop an element from the deque. The element is popped from the top of the deque and is removed from the same. Syntax: Array_Deque.pop() Parameters: The method does not take any parameters. Return Value: This method returns the element present at the front of the Deque. Exceptions: The method t
2 min read