PriorityQueue remove() Method in Java
Last Updated :
14 Dec, 2021
The remove() method of PriorityQueue class of java.util package is used to remove a particular element from a PriorityQueue. As we all know that the elements while entering into the priority queue are not sorted but as we all know while taking out elements from the priority queue the elements are always sorted being a trait of the priority queue. Here the default ordering of priority of elements for data types is defined as follows:
- Integer: Smallest elements that come first (while dealing with positive numbers only)
- String: Alphabetical ordering
Note: We can also insert a Comparator while creating an instance of this class which tells us how the priority should be defined.
Syntax:
PriorityQueue<String> = new PriorityQueue<String>(ComparatorHere);
Syntax: Remove method
Priority_Queue.remove(Object O)
Parameters: The parameter O is of the type of PriorityQueue and specifies the element to be removed from the PriorityQueue.
Return Value: This method returns True if the specified element is present in the Queue else it returns False.
Example 1
Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
PriorityQueue<String> queue
= new PriorityQueue<String>();
queue.add( "Welcome" );
queue.add( "To" );
queue.add( "Geeks" );
queue.add( "For" );
queue.add( "Geeks" );
System.out.println( "Initial PriorityQueue: "
+ queue);
queue.remove( "Geeks" );
queue.remove( "For" );
queue.remove( "Welcome" );
System.out.println( "PriorityQueue after removing "
+ "elements: " + queue);
}
}
|
Output
Initial PriorityQueue: [For, Geeks, To, Welcome, Geeks]
PriorityQueue after removing elements: [Geeks, To]
Example 2
Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
PriorityQueue<Integer> queue
= new PriorityQueue<Integer>();
queue.add( 10 );
queue.add( 15 );
queue.add( 30 );
queue.add( 20 );
queue.add( 5 );
System.out.println( "Initial PriorityQueue: "
+ queue);
queue.remove( 30 );
queue.remove( 5 );
System.out.println( "PriorityQueue after removing "
+ "elements: " + queue);
}
}
|
Output:
Initial PriorityQueue: [5, 10, 30, 20, 15]
PriorityQueue after removing elements: [10, 20, 15]
Geek, have you ever wondered what will happen if calls of remove() method exceed the elements present in the queue. In this scenario, it will continue to remove the elements that were there, and thereafter it will not find any element to remove priority-wise, so it will throw an exception which is as follows.
Note: This class do implements AbstractQueueInterface
Example
Java
import java.io.*;
import java.util.PriorityQueue;
class GFG {
public static void main(String[] args)
{
PriorityQueue<Integer> pq
= new PriorityQueue<Integer>();
pq.add( 2 );
pq.add( 14 );
pq.add( 41 );
pq.add( 7 );
pq.add( 99 );
System.out.println(
"Size of priority queue before deletion : "
+ pq.size());
System.out.println(
"Priority queue before removal : " + pq);
System.out.println( " 1st element removed : "
+ pq.remove());
System.out.println( " 2nd element removed : "
+ pq.remove());
System.out.println( " 3rd element removed : "
+ pq.remove());
System.out.println( " 4th element removed : "
+ pq.remove());
System.out.println( " 5th element removed : "
+ pq.remove());
System.out.println( " 6th element removed : "
+ pq.remove());
System.out.println(
"Priority queue after removal as follows: "
+ pq);
}
}
|
Output:
Output explanation:
It is showcasing that there are no further elements left in the queue as a queue is empty by now so does it throw NoSuchElementException.