w3resource

Java ArrayDeque Class: poll() Method

public E poll()

The poll() method is used to retrieve and remove the head of the queue represented by a given deque (in other words, the first element of this deque).

This method is equivalent to pollFirst().

Package: java.util

Java Platform: Java SE 8

Syntax:

poll()

Return Value:

the head of the queue represented by 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: poll() Method
Example: Java ArrayDeque Class: poll() 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.poll();
      System.out.println("Removed element: " + retval);

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

Output:

Powered by 
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:peekLast Method
Next:pollFirst 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_poll.php