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

ArrayDeque pollFirst() Method in Java

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

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

Syntax:

Array_Deque.pollFirst()

Parameters: The method does not take any parameter.

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

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




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


Output:

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

Program 2:




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


Output:

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

Program 3: For an empty deque:




// Java code to illustrate pollFirst()
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 first
        System.out.println("The element at head is: " + 
                                      de_que.pollFirst());
    }
}


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
TreeSet pollFirst() method in Java
The pollFirst() method of TreeSet in Java is used to retrieves and removes the first (lowest) element, or returns null if this TreeSet is empty. Syntax: E pollFirst() Where, E is the type of elements maintained by this TreeSet container. Parameters: This function does not accepts any parameter. Return Type:It returns the first element of the Treese
3 min read
LinkedBlockingDeque pollFirst() method in Java
The pollFirst() method of LinkedBlockingDeque returns the front element in the Deque container, and deletes it. It returns null if the container is empty. Syntax: public E pollFirst() Parameters: This method does not accept any parameters. Returns: This method returns front element in the Deque container if the container is not empty and deletes th
2 min read
ConcurrentLinkedDeque pollFirst() method in Java
The java.util.concurrent.ConcurrentLinkedDeque.pollFirst() is an in-built method in Java which retrieves the first element of this deque and removes it. If the deque is empty, the method returns NULL. Syntax: Conn_Linked_Deque.pollFirst() Parameters: This function does not accepts any parameter. Return Values: The function returns the first element
2 min read
ConcurrentSkipListSet pollFirst() method in Java
The pollFirst() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns retrieves and removes the first (lowest) element, or returns null if this set is empty. Syntax: public E pollFirst() Return Value: The function returns retrieves and removes the first (lowest) element, or returns null if this set is em
2 min read
NavigableSet pollFirst() method in Java
The pollFirst() method of NavigableSet interface in Java is used to retrieves and removes the first (lowest) element, or returns null if this set is empty. Syntax: E pollFirst() Where, E is the type of elements maintained by this Set container. Parameters: This function does not accepts any parameter. Return Value: It returns returns null if this s
1 min read
BlockingDeque pollFirst() method in Java with examples
The pollFirst() method of BlockingDeque returns the front element in the Deque container, and deletes it. It returns null if the container is empty. Syntax: public E pollFirst() Parameters: This method does not accept any parameters. Returns: This method returns front element in the Deque container if the container is not empty and deletes the elem
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