PriorityBlockingQueue clear() method in Java

The clear() method of PriorityBlockingQueue removes all the elements from this queue. Therefore this method can be applied when it is required to clear the PriorityBlockingQueue.

Syntax:

public void clear()

Parameter:
This method takes no parameters.



Returns:
This method returns nothing.

Exception:
This method does not throw any Exception.

Below program illustrate removing all the elements from PriorityBlockingQueue using clear() method.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Demonstrate clear() method
// of PriorityBlockingQueue.
  
import java.util.concurrent.PriorityBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacity = 15;
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<Integer> PrioBlockingQueue
            = new PriorityBlockingQueue<Integer>(capacity);
  
        // add numbers
        PrioBlockingQueue.add(78758575);
        PrioBlockingQueue.add(63447688);
        PrioBlockingQueue.add(56434788);
  
        // print queue after add operation
        System.out.println("After Adding  Numbers:");
        System.out.println("PriorityBlockingQueue:"
                           + PrioBlockingQueue);
  
        // remove all the elements using clear() method
        PrioBlockingQueue.clear();
  
        // print queue after clear operation
        System.out.println("\nAfter clear operation:");
        System.out.println("PriorityBlockingQueue:"
                           + PrioBlockingQueue);
    }
}

chevron_right


Output:

After Adding  Numbers:
PriorityBlockingQueue:[56434788, 78758575, 63447688]

After clear operation:
PriorityBlockingQueue:[]

Example 2: To illustrate clear method on a PriorityBlockingQueue which contains a list of names.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to Demonstrate clear() method
// of PriorityBlockingQueue.
  
import java.util.concurrent.PriorityBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // define capacity of PriorityBlockingQueue
        int capacity = 15;
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<String> PrioBlockingQueue
            = new PriorityBlockingQueue<String>(capacity);
  
        // add some names
        PrioBlockingQueue.add("Tandrima");
        PrioBlockingQueue.add("Argha");
        PrioBlockingQueue.add("Arka");
  
        // print queue after add operation
        System.out.println("List of Names:");
        System.out.println("PriorityBlockingQueue: "
                           + PrioBlockingQueue);
  
        // remove all the elements using clear() method
        PrioBlockingQueue.clear();
  
        // print queue after clear operation
        System.out.println("\nAfter clearing List of names:");
        System.out.println("PriorityBlockingQueue:"
                           + PrioBlockingQueue);
    }
}

chevron_right


Output:

List of Names:
PriorityBlockingQueue: [Argha, Tandrima, Arka]

After clearing List of names:
PriorityBlockingQueue:[]

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.