Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is ConcurrentLinkedDeque.remove(Object) in Java?

Programming Bytes

ConcurrentLinkedDeque is a thread-safe, unbounded deque. The null value is not permitted as an element. We can use ConcurrentLinkedDeque when multiple threads share a single deque.

The remove method of the ConcurrentLinkedDeque class loops through the deque elements and removes the first element that matches the element passed as a parameter.

Syntax

public boolean remove(Object obj)

Parameters

This method takes the element to be removed from the deque object as a parameter.

Return value

The remove method returns true if the passed argument is present in the deque. Otherwise, it returns false.

Code

The code below demonstrates how to use the remove method.

import java.util.concurrent.ConcurrentLinkedDeque;
class Remove {
    public static void main( String args[] ) {
        // Creating dequeu
        ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();

        // add elememts 
        deque.add("hi");
        deque.add("hello");
        deque.add("hi");

        System.out.println("The deque is: " + deque);
        // remove element
        deque.remove("hi");
        System.out.println("\nAfter removing element 'hi'. The deque is: " + deque);

    }
}
An example of the ConcurrentLinkedDeque.remove() method

Explanation

In the code above:

  • In line 5: We create a ConcurrentLinkedDeque object with name deque.

  • In lines 8 to 10: We add three elements (hi, hello, hi) to the created deque object.

  • In line 14: We use the remove method of the deque object to remove the element hi. The element hi is present at index 0 and 2. The remove method will delete the first occurrence of hi, so the element hi at index 0 is deleted.

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring