ConcurrentLinkedDeque
is a thread-safe, unbounded deque. Thenull
value is not permitted as an element. We can useConcurrentLinkedDeque
when multiple threads share a single deque.
The pollLast
method retrieves and removes the last element of the ConcurrentLinkedDeque
object.
public E pollLast()
This method doesn’t take any parameters.
This method retrieves and removes the last element of the Deque
object. If the Deque
object is empty, then the pollLast
method returns null
.
This method is similar to the
removeLast
method, except theremoveLast
method throws theNoSuchElementException
if thedeque
is empty, whereas thepollLast
method returnsnull
.
The example below demonstrates the use of the pollLast
method. Click on the “Run” button to execute the given code.
import java.util.concurrent.ConcurrentLinkedDeque;class PollLast {public static void main( String args[] ) {ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();deque.add("1"); // adding elementsdeque.add("2");deque.add("3");System.out.println("The deque is " + deque);// calling pollLast methodSystem.out.println("deque.pollLast() returns : " + deque.pollLast());System.out.println("The deque is " + deque);}}
In line #1, we import the ConcurrentLinkedDeque
class.
In line #4, we create a ConcurrentLinkedDeque
object named deque
.
From line #5 to 7, we use the deque
object to add three elements ("1"
,"2"
,"3"
) to deque
.
In line #10, we use the pollLast()
method of the deque
object to retrieve and remove the last element. In our case, 3
will be removed and returned.