w3resource

Java ArrayDeque Class: pollFirst() Method

public E pollFirst()

The pollFirst() method is used to retrieve and remove the first element of a given deque.

Package: java.util

Java Platform: Java SE 8

Syntax:

pollFirst()

Return Value:

the head of this deque, or null if this deque is empty

Return Value Type: E - the type of elements held in this collection

Pictorial Presentation

Java ArrayDeque Class: pollFirst() Method

Example: Java ArrayDeque Class: pollFirst() Method

import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
   public static void main(String[] args) {
      
      // Create an array deque 
      Deque<Integer> deque = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque.add(100);
      deque.add(200);
      deque.add(150);
      deque.add(95);        

      // Print all the elements of the original deque
      System.out.println("Elements of the original deque:");
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }

      int retval = deque.pollFirst();
      System.out.println("Removed element: " + retval);

      // printing all the elements available in deque after using pollFirst()
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }
   }
}

Output:

Elements of the original deque:
Number = 100
Number = 200
Number = 150
Number = 95
Removed element: 100
Number = 200
Number = 150
Number = 95

Java Code Editor:

Previous:poll Method
Next:pollLast Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_pollfirst.php