PriorityBlockingQueue add() Method in Java Last Updated : 13 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 mandatory parameter e which is the element to be inserted in PriorityBlockingQueue. Returns: This method returns a boolean response. It returns true if the adding of the element is successful, else it returns false. Exception: This method throws following exceptions: ClassCastException: if the element passed as parameter cannot be compared to the element contained by queue to keep priority queue's ordering.NullPointerException: if the element passed as parameter is null. Below program illustrate add() method of PriorityBlockingQueue: Example 1: Java // Java Program to Demonstrate add(E e) 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(526734); PrioBlockingQueue.add(84879456); PrioBlockingQueue.add(4586415); // print queue after add operation System.out.println("After Adding Some Numbers"); System.out.println("PriorityBlockingQueue:" + PrioBlockingQueue); // add more numbers PrioBlockingQueue.add(156116); PrioBlockingQueue.add(61651191); // print queue after add operation System.out.println("\nAfter adding Some More Numbers"); System.out.println("PriorityBlockingQueue:" + PrioBlockingQueue); } } Output:After Adding Some Numbers PriorityBlockingQueue:[526734, 84879456, 4586415] After adding Some More Numbers PriorityBlockingQueue:[156116, 526734, 4586415, 84879456, 61651191] Example 2: To demonstrate NullPointerException thrown by add() method. Java // Java Program to Demonstrate Exception // thrown by add(E e) 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(526734); PrioBlockingQueue.add(84879456); PrioBlockingQueue.add(4586415); try { // try to add null to PrioBlockingQueue PrioBlockingQueue.add(null); // print PrioBlockingQueue after add operation System.out.println("PriorityBlockingQueue:" + PrioBlockingQueue); } catch (Exception e) { System.out.println("Exception when adding null: " + e); } } } Output:Exception when adding null: java.lang.NullPointerException Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#add-E- Comment More infoAdvertise with us Next Article PriorityBlockingQueue drainTo() method in Java A AmanSingh2210 Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-PriorityBlockingQueue +1 More Practice Tags : JavaJava-Collections Similar Reads 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: 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 PriorityQueue add() Method in Java The Java.util.PriorityQueue.add() method in Java is used to add a specific element into a PriorityQueue. This method internally just calls the Java.util.PriorityQueue.offer() method with the value passed to it. So, it exactly works like offer() method. Syntax: Priority_Queue.add(Object element) Para 2 min read PriorityBlockingQueue take() method in Java The take() method of PriorityBlockingQueue returns head of the queue after removing it. If queue is empty, then this method will wait until an element becomes available. Syntax: public E take() throws InterruptedException Returns: This method returns value at the head of this PriorityBlockingQueue. 2 min read PriorityBlockingQueue size() method in Java The size() method of PriorityBlockingQueue is used to find the present size of the queue. It returns the number of elements in the collection. If the collection contains more than Integer.MAX_VALUE elements, then this method returns Integer.MAX_VALUE. Syntax: public int size() Return Value: This met 2 min read Like