Horje
Java Program to Handle Duplicate Elements in a PriorityQueue While Maintaining Order

In Java, elements in a Priority Queue are arranged according to their priority, which is based on a priority heap. When there are duplicate items, the default comparison method is to use a comparator that is given or to compare the elements according to their natural ordering. We can use a custom comparator or wrapper class to handle duplicates and preserve order.

In this article, we will learn how to handle duplicate elements in a PriorityQueue while maintaining order in Java.

Approaches to Handle Duplicates in a Priority Queue

By using two methods we can handle duplicate elements in a Priority Queue while maintaining order.

  • Using a Custom Comparator
  • Using a Wrapper Class

Program to Handle Duplicate Elements in a PriorityQueue

Approach 1: Using a Custom Comparator

Below is the implementation of Handling Duplicate Elements in a PriorityQueue:

Java

// Java program to handle duplicate elements in a PriorityQueue while maintaining order 
// using a custom comparator
import java.util.Comparator;
import java.util.PriorityQueue;
  
public class PriorityQueueExample {
    public static void main(String[] args) {
        // Custom comparator to maintain order for duplicates
        Comparator<Integer> customComparator = (a, b) -> {
            int result = Integer.compare(a % 10, b % 10);
            return result == 0 ? Integer.compare(a, b) : result;
        };
  
        // PriorityQueue with custom comparator
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(customComparator);
  
        // Adding elements to PriorityQueue
        priorityQueue.add(15);
        priorityQueue.add(22);
        priorityQueue.add(11);
        priorityQueue.add(22); // Duplicate element
        priorityQueue.add(30);
          
        // Polling elements from PriorityQueue
        while (!priorityQueue.isEmpty()) {
            System.out.println(priorityQueue.poll());
        }
    }
}

Output

30
11
22
22
15

Explanation of the above Program:

  • In this example, we specify a particular ordering for the entries in the PriorityQueue by creating a custom comparator.
  • The comparator evaluates and contrasts each element’s last digit. It compares the items directly if the last digits are the same.
  • In this manner, the custom comparator determines the order of duplicate items when they are encountered.
  • After that, elements including duplicates are added to the PriorityQueue and polled in the proper sequence.

Approach 2: Using a Wrapper Class

Below is the implementation of the a Wrapper Class for handling elements in PriorityQueue:

Java

// Java program to handle duplicate elements in a PriorityQueue while maintaining order
// using a wrapper class
import java.util.PriorityQueue;
public class ElementWrapper implements Comparable<ElementWrapper> 
{
    int value;
  
    public ElementWrapper(int value) 
    {
        this.value = value;
    }
  
    @Override
    public int compareTo(ElementWrapper other) {
        return Integer.compare(this.value % 10, other.value % 10);
    }
  
    public static void main(String[] args) {
        // PriorityQueue with ElementWrapper
        PriorityQueue<ElementWrapper> priorityQueue = new PriorityQueue<>();
  
        // Adding elements to PriorityQueue
        priorityQueue.add(new ElementWrapper(15));
        priorityQueue.add(new ElementWrapper(22));
        priorityQueue.add(new ElementWrapper(11));
        priorityQueue.add(new ElementWrapper(22)); // Duplicate element
        priorityQueue.add(new ElementWrapper(30));
  
        // Polling elements from PriorityQueue
        while (!priorityQueue.isEmpty()) {
            System.out.println(priorityQueue.poll().value);
        }
    }
}

Output

30
11
22
22
15

Explanation of the above Program:

  • In this example we provide a wrapper class that implements the Comparable interface called ElementWrapper.
  • The wrapper class’s compareTo function is modified to compare components according to the last digit.
  • Next, ElementWrapper objects are used to instantiate the PriorityQueue, guaranteeing that duplicate items are identified according to their inherent ordering.
  • When duplicates are present, the PriorityQueue is filled, and items are polled to display the maintained order.



Reffered: https://www.geeksforgeeks.org


Java

Related
How to Convert an ArrayList into a JSON String in Java? How to Convert an ArrayList into a JSON String in Java?
How to Convert a HashSet to JSON in Java? How to Convert a HashSet to JSON in Java?
Extract a Substring Between Two Characters in a String in Java Extract a Substring Between Two Characters in a String in Java
How to Convert an ArrayList Containing Integers to Primitive Int Array? How to Convert an ArrayList Containing Integers to Primitive Int Array?
Array with Constant Time Insertions and Deletions in Java Array with Constant Time Insertions and Deletions in Java

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13