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

A

PriorityQueueis an abstract data type that is similar to a queue, and every element has some priority value associated with it.

It is used when the objects are supposed to be processed based on priority. The elements of the priority queue are ordered according to the natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.

### Operations on a Priority Queue:

A typical priority queue supports the following operations:

**Insertion in a Priority Queue:** A priority queue is a data structure that stores elements with associated priorities and provides operations to insert new elements and remove the element with the highest priority. In a priority queue, the highest priority element is always at the front of the queue, and new elements are inserted in such a way that the order of priorities is preserved.

When a new element is inserted into a priority queue, it is first placed in the next available slot from top to bottom and left to right. This is done to maintain a complete binary tree structure, where each level of the tree is completely filled, except for possibly the last level, which is filled from left to right.

However, this new element may not be in the correct position in terms of its priority. To ensure that the priority order is maintained, the element is compared with its parent node. If the element’s priority is higher than its parent’s, the two nodes are swapped.

After the swap, the process is repeated with the new parent node, comparing it with its parent node and swapping if necessary. This process continues until the new element is in the correct position in the priority queue.

Overall, the insertion process in a priority queue involves finding the correct position for the new element in terms of its priority and then ensuring that the priority order is maintained by swapping nodes as necessary. This ensures that the highest-priority element is always at the front of the queue, ready to be removed when needed.

**Deletion in a Priority Queue:**In a priority queue, elements are stored in a way that the element with the highest priority is at the front of the queue. A common implementation of a priority queue is a max heap, where the root node represents the highest priority element.

When an element is removed from the priority queue, the root node, which contains the highest priority element, is removed first. This removal creates an empty slot at the root of the tree, which needs to be filled with a new element to maintain the heap property.

To fill the empty slot, we can choose any leaf node of the tree, and replace the root node with that leaf node’s value. This leaf node can be chosen by traversing down the tree from the root node and choosing the larger of the two child nodes. This ensures that the maximum value is still at the root node after the replacement.

After the replacement, the tree may no longer satisfy the heap property, as the new root node may be smaller than its children. To restore the heap property, we can swap the new root node with its larger child until the heap property is restored. This process is known as heapify.

Overall, the deletion process in a priority queue involves removing the root node, filling the empty slot with a new element from a leaf node, and restoring the heap property through the heapify process. This ensures that the new root node contains the next highest priority element in the queue, ready to be removed when needed.

**Peek in a Priority Queue:** Peek is an operation in a priority queue that returns the highest priority element without removing it from the queue. The peek operation is useful when you want to check the next element that will be removed from the queue, without actually removing it.

In a max heap, the highest priority element is located at the root node, so the peek operation simply returns the value of the root node. Similarly, in a min heap, the minimum element is located at the root node, so the peek operation returns the value of the root node in that case.

Peek operation is an O(1) operation as it only involves accessing the value of the root node, which is located at the top of the heap. It does not require any modification of the heap structure or the elements within it.

Overall, the peek operation is useful when you want to check the next element that will be removed from the priority queue, without actually removing it. It is a quick and efficient way to access the highest or lowest priority element in the queue, depending on whether it is a max heap or a min heap.

### Modifying the First element:

Here, we are modifying elements at the first position of the priority queue(in decreasing order). We modify the first element of the PriorityQueue by removing it and adding a new element as shown below:

## Java

`// Java code to modify first element of priority queue` `import` `java.util.*;` `class` `GeeksforGeeks {` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `// Creating priorityqueue in decreasing order` ` ` `// Here, priority is given first to larger numbers` ` ` `PriorityQueue<Integer> pq = ` `new` `PriorityQueue<>(` ` ` `Collections.reverseOrder());` ` ` `// Adding elements into priorityqueue` ` ` `pq.offer(` `1` `);` ` ` `pq.offer(` `5` `);` ` ` `pq.offer(` `7` `);` ` ` `pq.offer(` `3` `);` ` ` `// Removing first element and` ` ` `// modifying it to 6` ` ` `System.out.println(` `"Original first element: "` ` ` `+ pq.poll());` ` ` `// Priorityqueue becomes 5, 3, 1` ` ` `// adding new element 6` ` ` `// priorityqueue becomes 6, 5, 3, 1` ` ` `pq.offer(` `6` `);` ` ` `// Printing modified first element` ` ` `System.out.println(` `"Modified first element: "` ` ` `+ pq.peek());` ` ` `pq.offer(` `9` `);` ` ` `System.out.println(` ` ` `"Again, re-modified first element: "` ` ` `+ pq.peek());` ` ` `}` `}` |

**Output**

Original first element: 7 Modified first element: 6 Again, re-modified first element: 9

**Time Complexity**: O(N*logN)**Auxiliary Space:** O(N)

### Modifying any element of a Custom Priority Queue:

For custom PriorityQueue, we first search and remove an element, make necessary modifications and reinsert it back.

Below is the implementation of the approach.

## Java

`// Java code to modify any element from priority queue` `import` `java.util.*;` `// Creating class Geek which stores name,` `// number of toffees of all Geeks` `class` `Geek {` ` ` `String name;` ` ` `int` `toffee;` ` ` `Geek(` `int` `toffee, String name)` ` ` `{` ` ` `this` `.name = name;` ` ` `this` `.toffee = toffee;` ` ` `}` `}` `// Sorting in ascending order of` `// number of toffees using comparator` `class` `sortBy ` `implements` `Comparator<Geek> {` ` ` `public` `int` `compare(Geek g1, Geek g2)` ` ` `{` ` ` `if` `(g1.toffee > g2.toffee) {` ` ` `return` `1` `;` ` ` `}` ` ` `else` `if` `(g1.toffee < g2.toffee) {` ` ` `return` `-` `1` `;` ` ` `}` ` ` `else` `{` ` ` `return` `0` `;` ` ` `}` ` ` `}` `}` `class` `GeeksforGeeks {` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `// Creating a priorityqueue` ` ` `PriorityQueue<Geek> pq` ` ` `= ` `new` `PriorityQueue<Geek>(` `new` `sortBy());` ` ` `// Adding elements to priorityqueue` ` ` `Geek g1 = ` `new` `Geek(` `2` `, ` `"ram"` `);` ` ` `Geek g2 = ` `new` `Geek(` `5` `, ` `"shyam"` `);` ` ` `Geek g3 = ` `new` `Geek(` `17` `, ` `"ramesh"` `);` ` ` `pq.offer(g1);` ` ` `pq.offer(g2);` ` ` `pq.offer(g3);` ` ` `// Printing original priorityqueue` ` ` `System.out.println(` `"Original PriorityQueue is: "` `);` ` ` `for` `(Geek g : pq) {` ` ` `System.out.println(g.name + ` `" "` `+ g.toffee);` ` ` `}` ` ` `// Checking if pq contains object g1` ` ` `if` `(pq.contains(g1)) {` ` ` `// Remove geek g1 from priorityqueue` ` ` `pq.remove(g1);` ` ` `// Modify toffee of g1 from 2 to 20` ` ` `g1.toffee = ` `20` `;` ` ` `// Again add g1 to priorityqueue` ` ` `pq.offer(g1);` ` ` `}` ` ` `// Printing modified priorityqueue` ` ` `System.out.println(` `"Modified PriorityQueue is: "` `);` ` ` `for` `(Geek g : pq) {` ` ` `System.out.println(g.name + ` `" "` `+ g.toffee);` ` ` `}` ` ` `}` `}` |

**Output**

Original PriorityQueue is: ram 2 shyam 5 ramesh 17 Modified PriorityQueue is: shyam 5 ramesh 17 ram 20

**Time Complexity**: O(N*logN)**Auxiliary Space:** O(N)

**Related Articles:**

## Please

Loginto comment...