Deque.poll()
method is present in the Deque
interface inside the java.util
package. It is used to retrieve and remove the head element of the given Deque. If the given Deque is empty, then this method returns null.
Let’s view the syntax of the function Dequeue.poll()
:
Deque.poll();
Deque.poll()
method doesn’t take any parameter.
Deque.poll()
method returns the first element of deque. On the other hand, returns null if the deque is empty.
Let us look at the code snippet below:
import java.util.*;class Solution {public static void main(String[] args) {Deque<Integer> dq= new ArrayDeque<>();dq.add(10);dq.add(20);dq.add(30);dq.add(40);dq.add(50);System.out.println("the elements of deque are:-\n"+dq);int ele= dq.poll();System.out.println("first element of deque is:-\n"+ ele);System.out.println("deque after poll operation is:-\n"+ dq);}}
java.util.*
package to include the Deque interface.Integer
type and, added elements 10,20,30,40,50 in that.deque.poll()
and storing the returned element in ele variable.poll()
method.In this way, we can use Deque.poll()
method in Java.