Home »
Java programming language
Java PriorityQueue remove() Method with Example
PriorityQueue Class remove() method: Here, we are going to learn about the remove() method of PriorityQueue Class with its syntax and example.
Submitted by Preeti Jain, on March 11, 2020
PriorityQueue Class remove() method
- remove() method is available in java.util package.
- remove() method is used to remove one instance of the given object from this PriorityQueue when exists.
- remove() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- remove() method does not throw an exception at the time of removing an object.
Syntax:
public boolean remove(Object ob);
Parameter(s):
- Object ob – represents the element (ele) to be remove from this PriorityQueue.
Return value:
The return type of the method is boolean, it returns true when the given element is to be removed successfully otherwise it returns false.
Example:
// Java program to demonstrate the example
// of boolean remove(Object ob) method of
// PriorityQueue
import java.util.*;
public class RemoveOfPriorityQueue {
public static void main(String args[]) {
// Instantiate PriorityQueue
PriorityQueue < String > pq = new PriorityQueue < String > ();
// By using add() method is add
// the given element into priority
// queue
pq.add("C");
pq.add("C++");
pq.add("JAVA");
pq.add("PHP");
pq.add("ANDROID");
pq.add("JAVA");
// Display PriorityQueue
System.out.println("PriorityQueue: " + pq);
// By using remove(object) method is to
// remove the single instance of given element
// from this PriorityQueue
Object ele = pq.remove("JAVA");
// Display removed element
System.out.println("pq.remove(JAVA): " + ele);
// Display PriorityQueue
System.out.println("PriorityQueue: " + pq);
}
}
Output
PriorityQueue: [ANDROID, C, JAVA, PHP, C++, JAVA]
pq.remove(JAVA): true
PriorityQueue: [ANDROID, C, JAVA, PHP, C++]