Friday, October 24, 2025
HomeLanguagesJavaConcurrentLinkedDeque clear() method in Java

ConcurrentLinkedDeque clear() method in Java

The java.util.concurrent.ConcurrentLinkedDeque.clear() method is an inbuilt method in Java which removes the elements in the Deque.

Syntax:

public void clear()

Parameters: The method do not accepts any parameter.

Return Value: The function does not return anything

Below programs illustrate the ConcurrentLinkedDeque.clear() method:

Program 1:




// Java Program to Demonstrate clear()
// method of ConcurrentLinkedDeque
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating an empty Deque
        ConcurrentLinkedDeque<String> cld
            = new ConcurrentLinkedDeque<String>();
  
        // Add elements into the Deque
        cld.add("Welcome");
        cld.add("To");
        cld.add("Geeks");
        cld.add("4");
        cld.add("Geeks");
  
        // Displaying the Deque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
  
        // Remove all elements of the Deque using clear()
        cld.clear();
  
        // Displaying message
        System.out.println("\nConcurrentLinkedDeque cleared\n");
  
        // Displaying the Deque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
    }
}


Output:

ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]

ConcurrentLinkedDeque cleared

ConcurrentLinkedDeque: []

Program 2:




// Java Program to Demonstrate clear()
// method of ConcurrentLinkedDeque
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating an empty Deque
        ConcurrentLinkedDeque<Integer> cld
            = new ConcurrentLinkedDeque<Integer>();
  
        // Add elements into the Deque
        cld.add(10);
        cld.add(20);
        cld.add(30);
        cld.add(40);
        cld.add(50);
  
        // Displaying the Deque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
  
        // Remove all elements of the Deque using clear()
        cld.clear();
  
        // Displaying message
        System.out.println("\nConcurrentLinkedDeque cleared\n");
  
        // Displaying the Deque
        System.out.println("ConcurrentLinkedDeque: "
                           + cld);
    }
}


Output:

ConcurrentLinkedDeque: [10, 20, 30, 40, 50]

ConcurrentLinkedDeque cleared

ConcurrentLinkedDeque: []

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#clear–

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS