Java PriorityQueue - offer() Method
The java.util.PriorityQueue.offer() method is used to insert the specified element into this priority queue.
Syntax
public boolean offer(E element)
Here, E is the type of element maintained by the container.
Parameters
element |
Specify element which need to be inserted in the priority queue. |
Return Value
true.
Exception
- Throws ClassCastException, if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering.
- Throws NullPointerException, if the specified element is null.
Example:
In the example below, the java.util.PriorityQueue.offer() method is used to insert the specified element into this priority queue.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a priority queue PriorityQueue<Integer> PQueue = new PriorityQueue<Integer>(); //populating the priority queue using offer method PQueue.offer(10); PQueue.offer(20); PQueue.offer(30); PQueue.offer(40); PQueue.offer(50); //printing the priority queue System.out.println("PQueue contains: " + PQueue); } }
The output of the above code will be:
PQueue contains: [10, 20, 30, 40, 50]
❮ Java.util - PriorityQueue