Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

What is Deque.pollFirst() method in Java?

Sumit Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In this shot, we will discuss the Deque.pollFirst() method in Java. It is present in the Deque interface inside the java.util package.

The Deque.pollFirst() method retrieves and removes the first element from the deque.

Syntax

Element pollFirst()

Paramter

The Deque.pollFirst() method doesn’t take any parameter.

Return

The Deque.pollFirst() method returns:

  • Element: The first element of the deque. It returns null if the deque is empty.

Code

Let’s look at the code snippet given below.

import java.util.*;
class Main 
{
  public static void main(String[] args)
  {
    Deque<Integer> d = new LinkedList<Integer>();

    d.add(286);
    d.add(425);
    d.add(289);
    d.add(410);
    d.add(2971);

    System.out.println("Elements in the deque are: "+d);
    System.out.println("Element removed from deque using Deque.pollFirst() method is: "+d.pollFirst());
  } 
}
Using Deque.pollFirst() method in Java

Explanation

  • In line 1, we imported the required package.
  • In line 2, we made a Main class.
  • In line 4, we made a main function.
  • In line 6, we declared a Deque consisting of Integer type elements.
  • From lines 8 to 12, we inserted values in the deque by using the Deque.add() method.
  • In line 14, we displayed the original deque elements.
  • In line 15, we used the Deque.pollFirst() method and displayed the removed first element from the deque with a message.

In this way, we can use the Deque.pollFirst() method in Java.

RELATED TAGS

communitycreator
java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring