PriorityBlockingQueue clear() method in Java Last Updated : 26 Nov, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } 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. Java // 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); } } 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-- Comment More infoAdvertise with us Next Article PriorityBlockingQueue clear() method in Java A AmanSingh2210 Follow Improve Article Tags : Java Java-Collections Java - util package java-basics Java-Functions Java-PriorityBlockingQueue +2 More Practice Tags : JavaJava-Collections Similar Reads PriorityBlockingQueue add() Method in Java The add(E e) method of PriorityBlockingQueue inserts the element passed as a parameter to the method at the tail of this PriorityBlockingQueue. This method returns true if the adding of the element is successful. Else it returns false. Syntax: public boolean add(E e) Parameter: This method takes a m 2 min read PriorityBlockingQueue comparator() method in Java The comparator() method of PriorityBlockingQueue returns the comparator that can be used to order the elements in a PriorityBlockingQueue. The method returns null value if the queue follows the natural ordering pattern of the elements. Syntax: public Comparator<? super E> comparator() Returns: 2 min read PriorityQueue clear() Method in Java The Java.util.PriorityQueue.clear() method is used to remove all the elements from a PriorityQueue. Using the clear() method only clears all the element from the queue and does not delete the queue. In other words, we can say that the clear() method is used to only empty an existing PriorityQueue. S 2 min read PriorityBlockingQueue put() method in Java The put(E e) method of PriorityBlockingQueue is used to add an element into this queue. This method inserts the specified element into this priority queue. Since the queue is unbounded, this method will be never be blocked.Syntax: public void put(E e) Parameter: This method accepts a mandatory param 2 min read PriorityBlockingQueue drainTo() method in Java The drainTo(Collection col) method of PriorityBlockingQueue removes all available elements from this LinkedBlocking Queue and adds them to the given collection passed as a parameter. drainTo(Collection<? super E> col) The drainTo(Collection<? super E> col) method of PriorityBlockingQueue 5 min read Like