Java PriorityBlockingQueue removeAll() Method Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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() Methodboolean 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] Comment More infoAdvertise with us Next Article Java PriorityBlockingQueue removeAll() Method J juhisrivastav Follow Improve Article Tags : Java Java-PriorityBlockingQueue Practice Tags : Java Similar Reads PriorityBlockingQueue remove() method in Java The remove(Object o) method of PriorityBlockingQueue is used to delete an element from this queue. This method removes a single instance of the element passed as the parameter, if it is present. It returns true if and only if the element was removed, else it returned false. Syntax: public boolean re 2 min read PriorityBlockingQueue take() method in Java The take() method of PriorityBlockingQueue returns head of the queue after removing it. If queue is empty, then this method will wait until an element becomes available. Syntax: public E take() throws InterruptedException Returns: This method returns value at the head of this PriorityBlockingQueue. 2 min read PriorityBlockingQueue size() method in Java The size() method of PriorityBlockingQueue is used to find the present size of the queue. It returns the number of elements in the collection. If the collection contains more than Integer.MAX_VALUE elements, then this method returns Integer.MAX_VALUE. Syntax: public int size() Return Value: This met 2 min read PriorityQueue remove() Method in Java 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 al 5 min read PriorityBlockingQueue poll() method in Java 1. poll() Method The poll() method of PriorityBlockingQueue retrieves and removes element from head of this PriorityBlockingQueue. This method returns the element it removes from PriorityBlockingQueue but when the queue is empty then method will return null. Syntax: public E poll() Returns: This met 4 min read Like