Java PriorityQueue : remove()

By Arvind Rai, December 24, 2022
On this page, we will learn to remove elements from PriorityQueue in our Java application.
1. The java.util.PriorityQueue is an unbounded priority queue based on a priority heap.
2. The elements of the PriorityQueue are ordered according to their natural ordering or by specified Comparator in the constructor while creating it.
3. The elements can be removed from PriorityQueue using its following methods.
remove()
remove(Object ob)
removeAll(Collection<?> c)
removeIf(Predicate<? super E> filter) 
Now find the example one-by-one.

1. Remove Head of the Queue

Find the method declaration from Java doc.
public E remove() 
Retrieves and removes the head of this queue.
Returns:
The head of this queue.
Throws:
If this queue is empty, NoSuchElementException is thrown.
Example:
PriorityQueue<Stringgt; pq = new PriorityQueue<gt;();
pq.add("Mohan");
pq.add("Krishn");
pq.add("Abhay");
System.out.println(pq);
String deleted = pq.remove();
System.out.println(deleted);
System.out.println(pq); 
Output
[Abhay, Mohan, Krishn]
Abhay
[Krishn, Mohan] 
We are adding string elements in our priority queue. The elements will be ordered according to their natural ordering and the element "Abhay" will be on the head of the queue.
The remove() method retrieves and removes the head of the queue. So we can see that the element "Abhay" is retrieved and removed from this queue.

2. Remove Specified Element

Find the method declaration from Java doc.
public boolean remove(Object ob) 
Removes the single instance of specified element from this priority queue. If there are more than one such elements, the method will remove only single element.
Parameters:
The ob is element to be removed from this queue, if present.
Returns:
true if an element is removed.
Example:
PriorityQueue<String> pq = new PriorityQueue<>();
pq.add("Mohan");
pq.add("Krishn");
pq.add("Abhay");
System.out.println(pq);
pq.remove("Mohan");
System.out.println(pq); 
Output
[Abhay, Mohan, Krishn]
[Abhay, Krishn] 
If the element that needs to be removed, is more than one such elements in the queue, only one element is removed at one call.

3. Remove All Elements of Specified Collection

Find the method declaration from Java doc.
public boolean removeAll(Collection<?> c) 
Removes all the elements that are present in the specified collection. If an element present in the specified collection is repeating in the queue, then all such elements will be removed.
Parameters:
The c is collection containing elements to be removed from this queue.
Returns:
true, if this queue changed as a result of the call.
Throws:
NullPointerException - if the specified collection contains null elements or if the specified collection is null.
Example:
PriorityQueue<String> pq = new PriorityQueue<>();
pq.add("Mohan");
pq.add("Krishn");
pq.add("Abhay");
pq.add("Mohan");	
pq.add("Santosh");
List<String> list = Arrays.asList("Mohan", "Abhay");
System.out.println(pq);
pq.removeAll(list);
System.out.println(pq); 
Output
[Abhay, Mohan, Krishn, Mohan, Santosh]
[Krishn, Santosh] 

4. Remove Elements by Specified Predicate

Find the method declaration from Java doc.
public boolean removeIf(Predicate<? super E> filter) 
Removes all of the elements of this queue that satisfy the given predicate.
Parameters:
The filter is a predicate which returns true for elements to be removed.
Returns:
true, if any element was removed.
Throws:
NullPointerException - if the specified filter is null.
Example:
PriorityQueueRemoveIf.java
package com.concretepage;
import java.io.IOException;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.function.Predicate;

public class PriorityQueueRemoveIf {
  public static void main(String... args) throws Exception, IOException {
	Comparator<Person> nameComparator = Comparator.comparing(Person::getName);
	PriorityQueue<Person> pq = new PriorityQueue<>(nameComparator);
	Person p1 = new Person("Mohan", 25);
	Person p2 = new Person("Krishn", 30);
	Person p3 = new Person("Abhay", 32);
	
	pq.add(p1);
	pq.add(p2);
	pq.add(p3);
	
	Predicate<Person> personToRemove = p -> p.getAge() >= 25 && p.getAge() <= 30;
	
	pq.removeIf(personToRemove);
	System.out.println(pq);
  }
} 
Person.java
package com.concretepage;
public class Person {
  private String name;
  private int age;
  public Person(String name, int age) {
	this.name = name;
	this.age = age;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  @Override
  public String toString() {
    return name + " - " + age; 
  }
} 
Output
[Abhay - 32] 

5. Reference

Class PriorityQueue
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us