Java PriorityQueue retainAll() Method



Description

The java PriorityQueue retainAll(Collection<?> c) method retains all of common elements present in the arraydeque object and the provided collection's elements and remove the other remaining elements. This method modifies the arraydeque object.

Declaration

Following is the declaration for java.util.PriorityQueue.retainAll(Collection<?> c) method

public boolean retainAll​(Collection<?> c)

Parameters

c − The collection containing elements to be retained from this collection.

Return Value

true if this arraydeque is changed as a result of the call.

Exception

NullPointerException − if this arraydeque contains one or more null elements and the specified collection does not support null elements, or if the specified collection is null.

Retaining All Given Elements of a PriorityQueue of Ints Example

The following example shows the usage of Java PriorityQueue retainAll() method with Integers. We're creating an PriorityQueue of Integers, adding some elements, print it and then use retainAll() method to retain few elements. As PriorityQueue is modified it is printed to check if specified elements are retained or not.

package com.tutorialspoint;

import java.util.PriorityQueue;
import java.util.Arrays;

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(25);
      queue.add(30);
      queue.add(20);
      queue.add(18);        

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

      // it will retain two common elements
      System.out.println("PriorityQueue modified:  " + queue.retainAll(Arrays.asList(11,30,20,12)));
	  
      // 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 = [25, 30, 20, 18]
PriorityQueue modified:  true
PriorityQueue = [30, 20]

Retaining All Given Elements of a PriorityQueue of Strings Example

The following example shows the usage of Java PriorityQueue retainAll() method with Strings. We're creating an PriorityQueue of Strings, adding some elements, print it and then use retainAll() method to retain few elements. As PriorityQueue is modified it is printed to check if specified elements are retained or not.

package com.tutorialspoint;

import java.util.PriorityQueue;
import java.util.Arrays;

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("B");
      queue.add("C");
      queue.add("D");        

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

      // it will retain two common elements
      System.out.println("PriorityQueue modified:  " + queue.retainAll(Arrays.asList("E","B","C","F")));
	  
      // 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, B, C, D]
PriorityQueue modified:  true
PriorityQueue = [B, C]

Retaining All Given Elements of a PriorityQueue of Objects Example

The following example shows the usage of Java PriorityQueue retainAll() method with Student objects. We're creating an PriorityQueue of Student objects, adding some elements, print it and then use retainAll() method to retain few students. As PriorityQueue is modified it is printed to check if specified students are retained or not.

package com.tutorialspoint;

import java.util.PriorityQueue;
import java.util.Arrays;

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 return true after retaining two students from queue
      System.out.println("Student removed : " + queue.retainAll(
         Arrays.asList(new Student(2, "Robert"),new Student(3, "Adam"))));
	  
      // 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 ]]
Student removed : true
PriorityQueue = [[ 2, Robert ], [ 3, Adam ]]
java_util_priorityqueue.htm
Advertisements