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:
// 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); } } |
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.
// 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); } } |
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–
Recommended Posts:
- PriorityBlockingQueue add() Method in Java
- PriorityBlockingQueue put() method in Java
- PriorityBlockingQueue contains() method in Java
- PriorityBlockingQueue take() method in Java
- PriorityBlockingQueue poll() method in Java
- PriorityBlockingQueue drainTo() method in Java
- PriorityBlockingQueue comparator() method in Java
- PriorityBlockingQueue offer() method in Java
- PriorityBlockingQueue toString() method in Java
- PriorityBlockingQueue size() method in Java
- PriorityBlockingQueue spliterator() method in Java
- PriorityBlockingQueue toArray() method in Java
- PriorityBlockingQueue remove() method in Java
- PriorityBlockingQueue peek() method in Java
- PriorityBlockingQueue iterator() method in Java
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.



