The
ConcurrentLinkedDequeis a thread-safe unbounded deque. Thenullvalue is not permitted as an element. We can use theConcurrentLinkedDequewhen multiple threads are sharing a single deque.
The removeFirst method gets and removes the first element of the ConcurrentLinkedDeque object.
public E removeFirst()
This method doesn’t take any parameters.
This method retrieves and removes the first element of the deque object. If the deque object is empty, then NoSuchElementException is thrown.
This method is similar to the
pollFirstmethod, except that theremoveFirstmethod throws theNoSuchElementExceptionif the deque is empty, whereas thepollFirstmethod returnsnull.
The code below demonstrates the use of the removeFirst method:
import java.util.concurrent.ConcurrentLinkedDeque;class RemoveFirst {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.removeFirst() returns : " + deque.removeFirst());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 line 5 to 7, we use the deque object to add three elements("1","2","3") to deque.
In line 10, we use the removeFirst() method of the deque object to get and remove the first element. In our case, 1 will be removed and returned.