Java PriorityBlockingQueue removeAll() Method
Last Updated :
23 Jul, 2025
In Java, the removeAll() method of the PriorityBlockingQueue class is used to remove all elements from the queue that are also present in another specified collection.
Example 1: The below Java program demonstrates the use of the removeAll() method to remove all the elements of integer type from a PriorityBlockingQueue.
Java
// Use of removeAll() in PriorityBlockingQueue
// of Integer type
import java.util.Arrays;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.function.Predicate;
class Geeks {
public static void main(String[] args)
{
// Creating a PriorityBlockingQueue of integers
PriorityBlockingQueue<Integer> q
= new PriorityBlockingQueue<>();
// Use add() to insert elements in the queue
q.add(10);
q.add(20);
q.add(30);
q.add(40);
q.add(50);
// Display the original PriorityBlockingQueue
System.out.println(
"Original PriorityBlockingQueue: " + q);
// Creating a list of elements
// to remove from the queue
PriorityBlockingQueue<Integer> r
= new PriorityBlockingQueue<>(
Arrays.asList(30, 50));
// Remove all elements that are in the r collection
q.removeAll(r);
// Display the PriorityBlockingQueue after removal
System.out.println("PriorityBlockingQueue after removeAll(): "
+ q);
}
}
OutputOriginal PriorityBlockingQueue: [10, 20, 30, 40, 50]
PriorityBlockingQueue after removeAll(): [10, 20, 40]
Syntax of PriorityBlockingQueue removeAll() Method
boolean removeAll(Collection c)
- Parameter: The collection "c" is the collection of elements to be removed from the queue.
- Return type: It returns true, if the elements were successfully removed. It returns false, if no elements were removed.
Example 2: Here, we will demonstrate the use of the removeAll() method to remove specific string elements from a PriorityBlockingQueue.
Java
// Use of removeAll() in PriorityBlockingQueue
// of String type
import java.util.concurrent.PriorityBlockingQueue;
public class Geeks {
public static void main(String[] args) {
// Creating a PriorityBlockingQueue of strings
PriorityBlockingQueue<String> q
= new PriorityBlockingQueue<>();
// Adding elements to the queue
q.add("A");
q.add("B");
q.add("C");
q.add("D");
q.add("E");
// Display the original PriorityBlockingQueue
System.out.println("Original PriorityBlockingQueue: " + q);
// Creating a collection of elements to remove
PriorityBlockingQueue<String> r
= new PriorityBlockingQueue<>();
r.add("B");
r.add("D");
// Removing elements from the queue
q.removeAll(r);
// Display the modified PriorityBlockingQueue
System.out.println("PriorityBlockingQueue after removeAll(): " + q);
}
}
OutputOriginal PriorityBlockingQueue: [A, B, C, D, E]
PriorityBlockingQueue after removeAll(): [A, C, E]
Explore
Basics
OOPs & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java