Java PriorityQueue remove() Method



Description

The java PriorityQueue remove() method retrieves and removes the head of the queue represented by this queue.

Declaration

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

public E remove()

Parameters

NA

Return Value

This method returns the head of the queue represented by this queue.

Exception

NoSuchElementException − if this queue is empty.

remove() method with Object as parameter

Description

The Java PriorityQueue remove(Object) method removes a single instance of the specified element from this queue.

Declaration

Following is the declaration for java.util.PriorityQueue.remove(o) method

public boolean remove(Object o)

Parameters

o − The element to be removed from this queue, if present

Return Value

This method returns true if this queue contains the specified element.

Exception

NA

Removing First Element from the PriorityQueue of Ints Example

The following example shows the usage of Java PriorityQueue remove() method with Integers. We're creating an PriorityQueue of Integers, adding some elements, print it and then use remove() method to remove the first element. As PriorityQueue is modified it is printed to check if first element 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<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 retrieve first element after removing from queue
      System.out.println("Retrieved Element is = " + queue.remove());
	  
      // 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 = [18, 20, 25, 30]
Retrieved Element is = 18
PriorityQueue = [20, 30, 25]

Removing First Element from the PriorityQueue of Strings Example

The following example shows the usage of Java PriorityQueue remove() method with Strings. We're creating an PriorityQueue of String, adding some elements, print it and then use remove() method to get the first element. As PriorityQueue is modified it is printed to check if first element 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<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 retrieve first element after removing from queue
      System.out.println("Retrieved Element is = " + queue.remove());
	  
      // 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]
Retrieved Element is = A
PriorityQueue = [B, D, C]

Removing First Element from the PriorityQueue of Objects Example

The following example shows the usage of Java PriorityQueue remove(object) method with Student objects. We're creating an PriorityQueue of Student, adding some elements, print it and then use remove(object) 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 return true after removing Robert from queue
      System.out.println("Student removed :" + queue.remove(new Student(2, "Robert")));
	  
      // 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 = [[ 1, Julie ], [ 3, Adam ]]
java_util_priorityqueue.htm
Advertisements