Wednesday, October 8, 2025
HomeLanguagesJavaArrayBlockingQueue clear() Method in Java

ArrayBlockingQueue clear() Method in Java

ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array.

  • ArrayBlockingQueue class is a member of the Java Collections Framework.
  • Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.
  • The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.
  • If you try to put an element into a full queue or to take an element from an empty queue then the queue will block you.

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

public void clear()

Below programs illustrate clear() method of ArrayBlockingQueue. Example 1 

Java




// Java Program Demonstrate clear()
// method of ArrayBlockingQueue.
 
import java.util.concurrent.ArrayBlockingQueue;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of ArrayBlockingQueue
        int capacity = 5;
 
        // create object of ArrayBlockingQueue
        ArrayBlockingQueue<Integer> queue
            = new ArrayBlockingQueue<Integer>(capacity);
 
        // Add element to ArrayBlockingQueue
        queue.add(23);
        queue.add(32);
        queue.add(45);
        queue.add(12);
 
        // print queue after adding numbers
        System.out.println("After adding numbers");
        System.out.println(queue);
 
        // Apply clear() method
        queue.clear();
 
        // print queue after clear() operation
        System.out.println("After clear() method");
        System.out.println(queue);
    }
}


Output :
After adding numbers
[23, 32, 45, 12]
After clear() method
[]

Example 2 

Java




// Java Program Demonstrate clear()
// method of ArrayBlockingQueue.
import java.util.concurrent.ArrayBlockingQueue;
 
public class GFG {
 
    public static void main(String[] args)
    {
        // define capacity of ArrayBlockingQueue
        int capacity = 5;
 
        // create object of ArrayBlockingQueue to store 5 names
        ArrayBlockingQueue<String> names
            = new ArrayBlockingQueue<String>(capacity);
 
        // Add element to ArrayBlockingQueue
        names.add("Aman");
        names.add("Siddhant");
        names.add("Mahafuj");
        names.add("Raunak");
        names.add("Suvo");
 
        // print queue after adding numbers
        System.out.println("List of Names: " + names);
        System.out.println("Remaining Capacity: " + names.remainingCapacity());
 
        // Apply clear() method
        names.clear();
 
        // print queue after clear() operation
        System.out.println("List of Names: " + names);
        System.out.println("Remaining Capacity: " + names.remainingCapacity());
    }
}


Output :
List of Names: [Aman, Siddhant, Mahafuj, Raunak, Suvo]
Remaining Capacity: 0
List of Names: []
Remaining Capacity: 5

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#clear()

RELATED ARTICLES

Most Popular

Dominic
32341 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6709 POSTS0 COMMENTS
Nicole Veronica
11874 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6783 POSTS0 COMMENTS
Umr Jansen
6786 POSTS0 COMMENTS