Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ConcurrentLinkedDeque.pollFirst() method in Java?

Programming Bytes

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.

ConcurrentLinkedDeque is a thread-safe, unbounded deque. The null value is not permitted as an element. We can use the ConcurrentLinkedDeque when multiple threads share a single deque.

The pollFirst() method gets and removes the first element of the ConcurrentLinkedDeque object.

Syntax

public E pollFirst()

Parameters

This method doesn’t take any parameters.

Return value

The pollFirst() method retrieves and removes the first element of the deque object. If the deque object is empty, then the method returns null.

This method is similar to the removeFirst() method, except that the removeFirst() method throws the NoSuchElementException if the deque is empty, whereas the pollFirst method returns null.

Code

The code below demonstrates the use of the pollFirst() method.

import java.util.concurrent.ConcurrentLinkedDeque;
class PollFirst {
public static void main( String args[] ) {
ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();
deque.add("1");
deque.add("2");
deque.add("3");
System.out.println("The deque is " + deque);
System.out.println("deque.pollFirst() returns : " + deque.pollFirst());
System.out.println("The deque is " + deque);
}
}

Explanation

In the code above:

  • In line 1: We import the ConcurrentLinkedDeque class.

  • In line 4: We create a ConcurrentLinkedDeque object named deque.

  • In lines 5-7: We use the deque object to add three elements ("1","2","3") to deque.

  • In line 10: We use the pollFirst() method of the deque object to get and remove the first element. In our case, 1 will be removed and returned.

RELATED TAGS

java
communitycreator

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