java.util.PriorityQueue.remove() Method
Advertisements
Description
The remove(Object o) method is used to remove a single instance of the specified element from this queue, if it is present.
Declaration
Following is the declaration for java.util.PriorityQueue.remove() method.
public boolean remove(Object o)
Parameters
o--The element to be removed from this queue.
Return Value
The method call returns 'true' if this queue changed as a result of the call.
Exception
NA
Example
The following example shows the usage of java.util.PriorityQueue.remove()
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 = 3; i < 10; i++ ){
prq.add (new Integer (i)) ;
}
System.out.println ( "Initial priority queue values are: "+ prq);
// remove 7 from the queue
boolean isremoved = prq.remove(7);
System.out.println ( "Return value after remove: "+ isremoved);
System.out.println ( "Priority queue values after remove: "+ prq);
}
}
Let us compile and run the above program, this will produce the following result.
Initial priority queue values are: [3, 4, 5, 6, 7, 8, 9] Return value after remove: true Priority queue values after remove: [3, 4, 5, 6, 9, 8]