Javatpoint Logo
Javatpoint Logo

Java PriorityQueue offer() Method

The offer() 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.

Specified By

The offer() method of the PrioriryQueue class is specified by offer() method n interface Queue<E>.

Return Value

The offer() method returns true same as defined by Queue.offer(E).

Throws

The offer() method throws:

  1. ClassCastException - if the specified element is not compared with the elements in this priority queue as per the priority queue's ordering.
  2. NullPointerException - if the specified element has null value.

Example 1

Test it Now

Output:

Priority Queue values are: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Priority Queue values after addition are: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 25]

Example 2

Test it Now

Output:

Priority Queue values are: [Aman, Asif, Dimple, Dhanil, Deepak, Harshit, Jass, Harman]
Exception in thread "main" java.lang.NullPointerException

Example 3

Test it Now

Output:

Amit
Barun
Raj
Simran
Next TopicJava PriorityQueue



Help Others, Please Share

facebook twitter pinterest