Java Program to Handle Duplicate Elements in a PriorityQueue While Maintaining Order Last Updated : 09 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 QueueBy using two methods we can handle duplicate elements in a Priority Queue while maintaining order. Using a Custom ComparatorUsing a Wrapper ClassProgram to Handle Duplicate Elements in a PriorityQueueApproach 1: Using a Custom ComparatorBelow 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()); } } } Output30 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 ClassBelow 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); } } } Output30 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. Comment More infoAdvertise with us Next Article Java Program to Handle Duplicate Elements in a PriorityQueue While Maintaining Order 21211adygf Follow Improve Article Tags : Java Java Programs priority-queue java-priority-queue Java Examples +1 More Practice Tags : Javapriority-queue Similar Reads How to Configure Java Priority Queue to Handle Duplicate Elements? In Java, PriorityQueue is the data structure that stores the elements based on their priorities and provides operations to efficiently retrieve and manipulate these elements based on their priorities. PriorityQueue handles the duplicate elements using a comparator. Comparator is a pre-defined interf 3 min read How to Handle Null Elements in a PriorityQueue in Java? In Java, a Priority Queue is an abstract data type similar to a regular queue or stack, but with a key difference like elements are dequeued based on their priority. Regarding the null value property, the PriorityQueue class does not allow null elements. Attempting to add a null element will result 3 min read Java Program to Implement PriorityQueue API A PriorityQueue is a linear data structure in which the elements are ordered according to their natural ordering or by some custom comparator provided at the queue at construction time. In PriorityQueue, the front of the queue points to the least element, and the rear points to the greatest element 4 min read How to Customize the Ordering of Elements in a PriorityQueue Using a Comparator in Java? A PriorityQueue is a data structure that allows elements to be processed based on their priority. By default, it orders elements according to their natural ordering (e.g., numeric values in ascending order). However, sometimes we need a different sorting order based on specific criteria. In this art 4 min read How to Copy Elements from One PriorityQueue to Another in Java? In Java, a priority queue is a data structure that allows the users to store data based on their priority. In this article, we will learn how to copy elements from one priority queue to another in Java. Example Input: PriorityQueue original ={1,2,3,4,5}Output: PriorityQueue Copy ={1,2,3,4,5}Copy ele 2 min read Like