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 excpetions:
- 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 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); } } |
Queue: [1, 2, 3, 4]
Program 2: To demonstrate NullPointerException
// 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); } } } |
Exception: java.lang.NullPointerException
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- PriorityBlockingQueue size() method in Java
- PriorityBlockingQueue remainingCapacity() method in Java
- PriorityBlockingQueue remove() method in Java
- PriorityBlockingQueue clear() method in Java
- PriorityBlockingQueue add() Method in Java
- PriorityBlockingQueue contains() method in Java
- PriorityBlockingQueue iterator() method in Java
- PriorityBlockingQueue peek() method in Java
- PriorityBlockingQueue take() method in Java
- PriorityBlockingQueue toString() method in Java
- PriorityBlockingQueue comparator() method in Java
- PriorityBlockingQueue drainTo() method in Java
- PriorityBlockingQueue spliterator() method in Java
- PriorityBlockingQueue toArray() method in Java
- PriorityBlockingQueue poll() method in Java
- PriorityBlockingQueue offer() method in Java
- PriorityBlockingQueue Class in Java
- Java.util.TreeMap.put() and putAll() in Java
- LinkedBlockingQueue put() method in Java with Examples
- HashMap put() 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.

