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.
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:
Queue: [1, 2, 3, 4]
Program 2: To demonstrate NullPointerException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.