In this section we will learn what the Deque poll(), pollFirst(), and pollLast() methods are and how to use them in Java.
What is Java Deque poll() Method?
The Java Deque poll() method is used to get the first (head) element of a Deque object and return that as a result.
Note: this method removes the first element from the list as well.
Java poll() Method Syntax:
E poll()
poll() Method Parameters:
The method does not take an argument.
poll() Method Return Value:
The return value of this method is the first element (the head element) of the target Deque object.
poll() Method Exceptions:
The method does not throw an exception.
Example: using Deque poll() method
import java.util.ArrayDeque; import java.util.Deque; class Main{ public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<>(); deque.add(10); deque.add(20); deque.add(30); deque.add(40); deque.add(50); System.out.println("The size of the Deque object before calling the poll method: "+deque.size()); System.out.println("Calling the method and here's the value it returned: "+deque.poll()); System.out.println("The size of the Deque object after calling the poll method: "+deque.size()); } }
Output:
The size of the Deque object before calling the poll method: 5 Calling the method and here's the value it returned: 10 The size of the Deque object after calling the poll method: 4
What is Java Deque pollFirst() Method?
The Java Deque pollFirst() method is used to return the first (head) element of a Deque object and return that as a result.
Note: just like the poll() method, this one also removes the head element from the list as well.
Java pollFirst() Method Syntax:
E pollFirst()
pollFirst() Method Parameters:
The method does not take an argument.
pollFirst() Method Return Value:
The return value of this method is the first (head) element of the target Deque object.
Note: if the target Deque is empty, the value null will return instead.
pollFirst() Method Exceptions:
The method does not throw an exception.
Example: using Deque pollFirst() method
import java.util.ArrayDeque; import java.util.Deque; class Main{ public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<>(); deque.add(30); deque.add(20); deque.add(10); deque.add(40); deque.add(50); System.out.println("The size of the Deque object before calling the pollFirst method: "+deque.size()); System.out.println("Calling the method and here's the value it returned: "+deque.pollFirst()); System.out.println("The size of the Deque object after calling the pollFirst method: "+deque.size()); } }
Output:
The size of the Deque object before calling the pollFirst method: 5 Calling the method and here's the value it returned: 30 The size of the Deque object after calling the pollFirst method: 4
What is Java Deque pollLast() Method?
The Java Deque pollLast() method is used to get and return the last (tail) element of a Deque object.
Note: this method will remove that element from the list as well.
Java pollLast() Method Syntax:
E pollLast()
pollLast() Method Parameters:
The method does not take an argument.
pollLast() Method Return Value:
The return value of this method is the last (tail) element of the target Deque object.
Note: if the target Deque object is empty, the value null will return instead.
pollLast() Method Exceptions:
The method does not throw an exception.
Example: using Deque pollLast() method
import java.util.ArrayDeque; import java.util.Deque; class Main{ public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<>(); deque.add(30); deque.add(20); deque.add(10); deque.add(40); deque.add(50); System.out.println("The size of the Deque object before calling the pollLast method: "+deque.size()); System.out.println("Calling the method and here's the value it returned: "+deque.pollLast()); System.out.println("The size of the Deque object after calling the pollLast method: "+deque.size()); } }
Output:
The size of the Deque object before calling the pollLast method: 5 Calling the method and here's the value it returned: 50 The size of the Deque object after calling the pollLast method: 4