Java PriorityQueue add() method

9 Nov 2024 | 2 min read

The add() method of the PriorityQueue class is used to insert the specified element into this priority queue.

Syntax

Parameter

e - It is the element to be added

Override

The add() method of class PriorityQueue overrides the add() method of class AbstractQueue<E>.

Return Value

The add() method returns true(as specified by Collection.add(E)).

Throws

The add() method throws :

  • ClassCastException - if the elements in this priority queue are not comparable with the specified element according to the priority queue's ordering.
  • NullPointerException - if the specified element is null.

Example 1

Output:

Course : C Course : C++ Course : Java Course : Python 

Example 2

Output:

1 Roll No = 2 Name = ravi Course = C Age = 21 2 Roll No = 1 Name = Geetanjali Course = Java Age = 21 3 Roll No = 3 Name = HimanshuBhardwaj Course = C++ Age = 21 

Example 3

Output:

Elements are : 800 1190 1178 7118 Max element = 7118 Min element = 800