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–

… [Trackback]
[…] There you will find 65825 more Info on that Topic: geeksforgeeks.org/priorityblockingqueue-clear-method-in-java/ […]
… [Trackback]
[…] Read More here to that Topic: geeksforgeeks.org/priorityblockingqueue-clear-method-in-java/ […]
… [Trackback]
[…] Read More on on that Topic: geeksforgeeks.org/priorityblockingqueue-clear-method-in-java/ […]
… [Trackback]
[…] Read More on that Topic: geeksforgeeks.org/priorityblockingqueue-clear-method-in-java/ […]