Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ConcurrentLinkedDeque.clear() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The clear method deletes all the elements of the ConcurrentLinkedDeque object.

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

Syntax

public void clear()

Parameters

This method doesn’t take any arguments.

Return value

The clear() method doesn’t return a value.

Code

The code below demonstrates the use of the clear method.

import java.util.concurrent.ConcurrentLinkedDeque;
class Clear {
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);
deque.clear();
System.out.println("After calling the clear method the deque is " + deque);
}
}
Example of the clear() method

Explanation

  • Line 1: We import ConcurrentLinkedDeque from the java.util.concurrent package.

  • Line 4: We create an object for the ConcurrentLinkedDeque class with the name deque.

  • Lines 5 to 7: We add three elements to the deque object.

  • Line 10: We call the clear method on the deque object. This removes all the elements from the deque and makes the deque empty.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring