PriorityBlockingQueue put() method in Java Last Updated : 01 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 parameter e which is the element to be inserted in PriorityBlockingQueue.Return Value: The method does not return anything.Exception: This method throws following exceptions: ClassCastException - if the specified element cannot be compared with elements currently in the priority queue according to the priority queue's ordering.NullPointerException - if the specified element is null. Below programs illustrate put() method in PriorityBlockingQueue:Program 1: Java // Java Program Demonstrate put(E e) // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; public class GFG { public static void main(String[] args) { // create object of PriorityBlockingQueue PriorityBlockingQueue<Integer> pbq = new PriorityBlockingQueue<Integer>(); // Add element using put() method pbq.put(1); pbq.put(2); pbq.put(3); pbq.put(4); // print elements of queue System.out.println("Queue: " + pbq); } } Output: Queue: [1, 2, 3, 4] Program 2: To demonstrate NullPointerException Java // Java Program Demonstrate put(E e) // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; public class GFG { public static void main(String[] args) { // create object of PriorityBlockingQueue PriorityBlockingQueue<String> pbq = new PriorityBlockingQueue<String>(); // try to put null value in put method try { pbq.put(null); } catch (Exception e) { // print error details System.out.println("Exception: " + e); } } } Output: Exception: java.lang.NullPointerException Comment More infoAdvertise with us Next Article PriorityBlockingQueue put() method in Java C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-PriorityBlockingQueue +1 More Practice Tags : JavaJava-Collections Similar Reads 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 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 toArray() method in Java toArray() The toArray method of PriorityBlockingQueue is used to create an array having the same elements as that of this PriorityBlockingQueue, in proper sequence. Actually, this method copies all the element from the PriorityBlockingQueue to a new array. This method behaves as a bridge between arr 4 min read PriorityBlockingQueue remove() method in Java The remove(Object o) method of PriorityBlockingQueue is used to delete an element from this queue. This method removes a single instance of the element passed as the parameter, if it is present. It returns true if and only if the element was removed, else it returned false. Syntax: public boolean re 2 min read Like