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

ArrayDeque pollLast() Method in Java

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

The java.util.ArrayDeque.pollLast() method in Java is used to retrieve or fetch and remove the last element of the Deque. The peekLast() method only retrieved the element at the end but the pollLast() also removes the element along with the retrieval. It returns NULL if the deque is empty.

Syntax:

Array_Deque.pollLast()

Parameters: The method does not take any parameter.

Return Value: The method removes the last element of the Deque and returns the same. It returns NULL if the deque is empty.

Below programs illustrate the Java.util.ArrayDeque.pollLast() method:
Program 1:




// Java code to illustrate pollLast()
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("4");
        de_que.add("Geeks");
  
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
  
        // Displaying the last element
        System.out.println("The last element is: " +
                                    de_que.pollLast());
  
        // Displaying the final ArrayDeque
        System.out.println("ArrayDeque after operation: "
                                                + de_que);
    }
}


Output:

ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The last element is: Geeks
ArrayDeque after operation: [Welcome, To, Geeks, 4]

Program 2:




// Java code to illustrate pollLast()
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("ArrayDeque: " + de_que);
  
        // Displaying the last element
        System.out.println("The element at head is: " +
                                       de_que.pollLast());
  
        // Displaying the final ArrayDeque
        System.out.println("ArrayDeque after operation: "
                                                + de_que);
    }
}


Output:

ArrayDeque: [10, 15, 30, 20, 5]
The element at head is: 5
ArrayDeque after operation: [10, 15, 30, 20]

Program 3: For an empty deque:




// Java code to illustrate pollLast()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        Deque<Integer> de_que = new ArrayDeque<Integer>();
  
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
  
        // Displaying the last element
        System.out.println("The element at head is: " +
                                       de_que.pollLast());
    }
}


Output:

ArrayDeque: []
The element at head is: null


Previous Article
Next Article

Similar Reads

Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
Java's Linked list class offers a function that allows a "Queue Based" working called poll(). This function not only returns deletes the first element, but also displays them while being deleted and hence can have a lot of usage in daily life problems and competitive programming as well. There are 3 variants of poll(), all three are discussed in th
4 min read
LinkedBlockingDeque pollLast() method in Java
The pollLast() method of LinkedBlockingDeque returns the last element in the Deque container, and deletes it. It returns null if the container is empty. Syntax: public E pollLast() Parameters: This method does not accept any parameters. Returns: This method returns last element in the Deque container if the container is not empty and deletes the el
2 min read
ConcurrentLinkedDeque pollLast() method in Java
The java.util.concurrent.ConcurrentLinkedDeque.pollLast() is an in-built method in Java which retrieves the last element of this deque and removes it. If the deque is empty, the method returns NULL. Syntax: Conn_Linked_Deque.pollLast() Parameters: The function accepts no parameters. Return Values:The function returns the last element of this deque.
2 min read
ConcurrentSkipListSet pollLast() method in Java
The pollLast() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns retrieves and removes the last (highest) element, or returns null if this set is empty. Syntax: public E pollLast() Return Value: The function returns retrieves and removes the last (highest) element, or returns null if this set is empt
2 min read
NavigableSet pollLast() method in Java
The pollLast() method of NavigableSet interface in Java is used to retrieve and remove the last (highest) element, or returns null if this set is empty. Syntax: E pollLast() Where, E is the type of elements maintained by this Set container. Parameters: This function does not accepts any parameter. Return Value: It returns the last (highest) element
1 min read
TreeSet pollLast() method in Java with Example
The pollLast() method of Java.util.concurrent.TreeSet is an in-built function in Java which returns retrieves and removes the last (highest) element, or returns null if this set is empty. Syntax: public E pollLast() Return Value: The function returns retrieves and removes the last (highest) element, or returns null if this set is empty. Below progr
2 min read
BlockingDeque pollLast() method in Java with examples
The pollLast() method of BlockingDeque returns the last element in the Deque container, and deletes it. It returns null if the container is empty. Syntax: public E pollLast() Parameters: This method does not accept any parameters. Returns: This method returns last element in the Deque container if the container is not empty and deletes the element
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