Java Program to Handle Duplicate Elements in a PriorityQueue While Maintaining Order
Last Updated :
09 Feb, 2024
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
import java.util.Comparator;
import java.util.PriorityQueue;
public class PriorityQueueExample {
public static void main(String[] args) {
Comparator<Integer> customComparator = (a, b) -> {
int result = Integer.compare(a % 10 , b % 10 );
return result == 0 ? Integer.compare(a, b) : result;
};
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(customComparator);
priorityQueue.add( 15 );
priorityQueue.add( 22 );
priorityQueue.add( 11 );
priorityQueue.add( 22 );
priorityQueue.add( 30 );
while (!priorityQueue.isEmpty()) {
System.out.println(priorityQueue.poll());
}
}
}
|
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
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<ElementWrapper> priorityQueue = new PriorityQueue<>();
priorityQueue.add( new ElementWrapper( 15 ));
priorityQueue.add( new ElementWrapper( 22 ));
priorityQueue.add( new ElementWrapper( 11 ));
priorityQueue.add( new ElementWrapper( 22 ));
priorityQueue.add( new ElementWrapper( 30 ));
while (!priorityQueue.isEmpty()) {
System.out.println(priorityQueue.poll().value);
}
}
}
|
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.
Share your thoughts in the comments
Please Login to comment...