The
ConcurrentLinkedDeque
is thread-safe unbounded deque. Thenull
value is not permitted as an element. We can use theConcurrentLinkedDeque
when multiple threads are sharing a single Deque.
The removeLast
method gets and removes the last element of the ConcurrentLinkedDeque
object.
public E removeLast()
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 NoSuchElementException
is thrown.
This method is similar to the
pollLast
method except that theremoveLast
method throws theNoSuchElementException
if thedeque
is empty whereas thepollLast
method returnsnull
.
The code below demonstrates the use of the removeLast
method:
import java.util.concurrent.ConcurrentLinkedDeque;class RemoveLast {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.removeLast() returns : " + deque.removeLast());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
.
From lines 5 to 7, we use the deque
object to add three elements("1"
,"2"
,"3"
) to deque
.
In line 10, we use the removeLast()
method of the deque
object to get and remove the last element. In our case, 3
will be removed and returned.