Monday, October 6, 2025
HomeLanguagesJavaLinkedBlockingQueue clear() method in Java

LinkedBlockingQueue clear() method in Java

The clear() method of LinkedBlockingQueue removes all of the elements from this queue. After applying this method the queue will become empty.

Syntax:

public void clear()

Below programs illustrates clear() method of LinkedBlockingQueue class:

Program 1:




// Java Program Demonstrate clear()
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 50;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<Integer> linkedQueue
            = new LinkedBlockingQueue<Integer>(capacityOfQueue);
  
        // Add element to LinkedBlockingQueue
        linkedQueue.add(2300);
        linkedQueue.add(1322);
        linkedQueue.add(8945);
        linkedQueue.add(6512);
  
        // print queue
        System.out.println("LinkedBlockingQueue before using"
                           + " clear() : " + linkedQueue);
  
        // Apply clear() method
        linkedQueue.clear();
  
        // print queue
        System.out.println();
        System.out.println("LinkedBlockingQueue after using"
                           + " clear() method : " + linkedQueue);
    }
}


Output:

LinkedBlockingQueue before using clear() : [2300, 1322, 8945, 6512]

LinkedBlockingQueue after using clear() method : []

Program 2:




// Java Program Demonstrate clear()
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 50;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<String> names
            = new LinkedBlockingQueue<String>(capacityOfQueue);
  
        // Add element to ArrayBlockingQueue
        names.add("Shubham");
        names.add("Siddhant");
        names.add("Mahafuj");
        names.add("Arka");
        names.add("Raunak");
  
        // print queue
        System.out.println("LinkedBlockingQueue before using"
                           + " clear() : " + names);
  
        // Apply clear() method
        names.clear();
  
        // print queue
        System.out.println();
        System.out.println("LinkedBlockingQueue after using"
                           + " clear() method : " + names);
    }
}


Output:

LinkedBlockingQueue before using clear() : [Shubham, Siddhant, Mahafuj, Arka, Raunak]

LinkedBlockingQueue after using clear() method : []

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

RELATED ARTICLES

Most Popular

Dominic
32338 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6707 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6825 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6782 POSTS0 COMMENTS