ConcurrentLinkedDeque
is a thread-safe, unbounded deque. Thenull
value is not permitted as an element. We can use theConcurrentLinkedDeque
when multiple threads share a single deque.
The pollFirst()
method gets and removes the first element of the ConcurrentLinkedDeque
object.
public E pollFirst()
This method doesn’t take any parameters.
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 theremoveFirst()
method throws theNoSuchElementException
if thedeque
is empty, whereas thepollFirst
method returnsnull
.
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);}}
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.