java.util.PriorityQueue.offer() Method
Advertisements
Description
The offer(E e) method is used to insert the specified element into this priority queue.
Declaration
Following is the declaration for java.util.PriorityQueue.offer() method.
public boolean offer(E e)
Parameters
e-- The element to add.
Return Value
The method call returns true (as specified by Queue.offer(E))
Exception
ClassCastException-- Throws if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering.
NullPointerException-- Throws if the specified element is null.
Example
The following example shows the usage of java.util.PriorityQueue.offer()
package com.tutorialspoint;
import java.util.*;
public class PriorityQueueDemo {
public static void main(String args[]) {
// create priority queue
PriorityQueue < Integer > prq = new PriorityQueue < Integer > ();
// insert values in the queue
for ( int i = 0; i < 10; i++ ){
prq.add (new Integer (i)) ;
}
System.out.println ( "Initial priority queue values are: "+ prq);
// add using offer() function call
prq.offer(122);
System.out.println ( "Priority queue values after addition: "+ prq);
}
}
Let us compile and run the above program, this will produce the following result.
Initial priority queue values are: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Priority queue values after addition: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 122]