How can I modify an element of a PriorityQueue in Java?


Generally, the Queue follows the First in First out (FIFO) approach but a PriorityQueue follows a priority based approach while accessing elements. Each element of the queue has a priority associated with it. The elements are prioritized based on natural sorting order. However, we can provide custom orders using a comparator. The elements of PriorityQueue are not actually sorted, they are only retrieved in sorted order. This feature allows us to modify an element of PriorityQueue easily.

Java Program to modify an element of a ProrityQueue

Before jumping into the program, let’s familiarize ourselves with a few in-built methods of PriorityQueue −

  • add() − It is used to add a single element to the queue

  • offer() − It also inserts a given element into the queue.

  • peek() − It is used to retrieve the first element of the queue.

  • remove() − It is used to remove the specified element from the queue.

Approach 1

  • Define an object of PriorityQueue collection and store a few elements using ‘add()’ method.

  • Now, using ‘peek()’ method show the first element of queue and then remove this element from queue using the ‘remove()’ method.

  • Moving further insert a new element at the same position using built-in method ‘offer()’.

  • Again show the modified first element.

Example

In the following example, we will modify an element of PriorityQueue. The elements are prioritized without a comparator which means they will be accessed in ascending order.

import java.util.*;
public class Modify {
  public static void main(String[] args) {
      PriorityQueue<Integer> queuePq = new PriorityQueue<>();
      
      // inserting elements
      queuePq.add(7);
      queuePq.add(9);
      queuePq.add(2);
      queuePq.add(4);
      queuePq.add(3);
      
      System.out.println("Original Queue: " + queuePq);
      int head1 = queuePq.peek(); // accessing first element
      
      System.out.println("The first element in Queue: " + head1);
      queuePq.remove(2); // removing first element
      queuePq.offer(1); // adding new element at first position
      int head2 = queuePq.peek(); // accessing first element
      
      System.out.println("The updated first element in Queue: " + head2);
      queuePq.offer(2); // adding new element at first position
      System.out.println("Newly updated Queue: " + queuePq);
   }
}

Output

Original Queue: [2, 3, 7, 9, 4]
The first element in Queue: 2
The updated first element in Queue: 1
Newly updated Queue: [1, 3, 2, 9, 4, 7]

Approach 2

  • Define an object of PriorityQueue collection using an in-built method named ‘Collections.reverseOrder()’ to store the elements in descending order.

  • Now, follow the same steps described in the previous example.

Example 

In the following example, we will modify an element of PriorityQueue. The elements are prioritized using a comparator to provide access in descending order.

import java.util.*;
public class Modify {
   public static void main(String[] args) {
      PriorityQueue<Integer> queuePq = new
      PriorityQueue<>(Collections.reverseOrder());
      
      // inserting elements
      queuePq.add(7);
      queuePq.add(9);
      queuePq.add(2);
      queuePq.add(1);
      queuePq.add(3);
      
      System.out.println("Original Queue: " + queuePq);
      int head1 = queuePq.peek(); // accessing first element
      
      System.out.println("The first element in Queue: " + head1);
      queuePq.remove(9); // removing first element
      queuePq.offer(8); // adding new element at first position
      int head2 = queuePq.peek(); // accessing first element
      
      System.out.println("The updated first element in Queue: " + head2);
      queuePq.offer(9); // adding new element at first position
      System.out.println("Newly updated Queue: " + queuePq);
   }
}     

Output

Original Queue: [9, 7, 2, 1, 3]
The first element in Queue: 9
The updated first element in Queue: 8
Newly updated Queue: [9, 7, 8, 1, 3, 2]

Conclusion

We started this article by defining the PriorityQueue class of Java Collection Framework that implements Queue Interface. In the next section, we discussed some of the in-built methods that are used in Java programs to modify an element of the given PriorityQueue.

Updated on: 20-Jul-2023

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements