What is ConcurrentLinkedDeque.removeLastOccurrence() in Java?
ConcurrentLinkedDequeis a thread-safe, unbounded deque. Thenullvalue is not permitted as an element. We can useConcurrentLinkedDequewhen multiple threads share a single deque.
The removeLastOccurrence method of the ConcurrentLinkedDeque class is used to remove the last occurrence of the specified element in the ConcurrentLinkedDeque object.
Syntax
public boolean removeLastOccurrence(Object o)
Parameters
This method takes the element to be removed from the deque as a parameter.
Return value
The removeLastOccurrence method returns true if the deque contains the specified element. Otherwise, it returns false.
Code
The code below demonstrates how to use the removeLastOccurrence() method.
import java.util.concurrent.ConcurrentLinkedDeque;class RemoveLastOccurrence {public static void main( String args[] ) {ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();deque.add("1");deque.add("2");deque.add("1");System.out.println("The deque is " + deque);System.out.println("Is element present & removed: " + deque.removeLastOccurrence("1"));System.out.println("The deque is " + deque);}}
Explanation
In the code above:
-
Line 1: We import the
ConcurrentLinkedDequeclass. -
Line 4: We create a
ConcurrentLinkedDequeobject with the namedeque. -
Lines 5-7: We use the
add()method of thedequeobject to add three elements ("1","2","3") to thedeque. -
Line 10: We use the
removeLastOccurrencemethod to remove the last occurrence of the element"1". The element"1"is present at two indexes,0and2. After we call this method, the element at index2will be removed.