Java PriorityQueue : remove()
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)
1. Remove Head of the Queue
Find the method declaration from Java doc.public E remove()
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);
[Abhay, Mohan, Krishn] Abhay [Krishn, Mohan]
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)
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);
[Abhay, Mohan, Krishn] [Abhay, Krishn]
3. Remove All Elements of Specified Collection
Find the method declaration from Java doc.public boolean removeAll(Collection<?> c)
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);
[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)
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); } }
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; } }
[Abhay - 32]