A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_poll.php below:

Website Navigation


Java ArrayDeque Class: poll() Method

Java ArrayDeque Class: poll() MethodLast update on August 19 2022 21:50:42 (UTC/GMT +8 hours) 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


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




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4