Java PriorityQueue removeIf() Method



Description

The java PriorityQueue removeIf() method retrieves and removes all the elements of this queue that satisfy the given predicate. In case of exception, the exception is relayed to the caller.

Declaration

Following is the declaration for java.util.PriorityQueue.removeIf(filter) method

public boolean removeIf​(Predicate<? super E> filter)

Parameters

filter − a predicate which returns true for elements to be removed.

Return Value

true if any elements were removed.

Exception

NullPointerException − if the specified filter is null.

Removing Element on Basis of a Condition in a PriorityQueue of Ints Example

The following example shows the usage of Java PriorityQueue removeIf(filter) method with Integers. We're creating an PriorityQueue of Integers, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As PriorityQueue is modified it is printed to check if even numbers are removed or not.

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<Integer> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(1);
      queue.add(2);
      queue.add(3);
      queue.add(4);
      queue.add(5);	  

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);

      // it will remove even numbers from the queue
      queue.removeIf(i -> i%2 == 0);
	  
      // let us print all the elements available in queue again
      System.out.println("PriorityQueue = " + queue);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

PriorityQueue = [1, 2, 3, 4, 5]
PriorityQueue = [1, 3, 5]

Removing Element on Basis of a Condition in a PriorityQueue of Strings Example

The following example shows the usage of Java PriorityQueue removeIf(filter) method with Strings. We're creating an PriorityQueue of String, adding some elements, print it and then use removeIf(filter) method to remove string with one character only. As PriorityQueue is modified it is printed to check if such elements are removed or not.

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<String> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add("A");
      queue.add("BB");
      queue.add("CC");
      queue.add("D");        

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);

      // it will remove single length string
      queue.removeIf(i -> i.length() == 1);
	  
      // let us print all the elements available in queue again
      System.out.println("PriorityQueue = " + queue);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

PriorityQueue = [A, BB, CC, D]
PriorityQueue = [BB, CC]

Removing Element on Basis of a Condition in a PriorityQueue of Objects Example

The following example shows the usage of Java PriorityQueue removeIf(filter) method with Student objects. We're creating an PriorityQueue of Student, adding some elements, print it and then use removeIf(filter) method to get the a particular student removed. As PriorityQueue is modified it is printed to check if that student object is removed or not.

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<Student> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(new Student(1, "Julie"));
      queue.add(new Student(2, "Robert"));
      queue.add(new Student(3, "Adam"));       

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);

      // it will remove if roll no is 2
      queue.removeIf(i -> i.rollNo == 2);
	  
      // let us print all the elements available in queue again
      System.out.println("PriorityQueue = " + queue);
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

Output

Let us compile and run the above program, this will produce the following result −

PriorityQueue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
PriorityQueue = [[ 1, Julie ], [ 3, Adam ]]
java_util_priorityqueue.htm
Advertisements