PriorityQueue comparator() Method in Java
The java.util.PriorityQueue.comparator() method shares an important function of setting and returning the comparator that can be used to order the elements in a PriorityQueue. The method returns Null value if the queue follows the natural ordering pattern of the elements.
Syntax:
comp_set = (PriorityQueue)Priority_Queue.comparator()
Parameters: The method does not take any parameters.
Return Value: The method returns the comparator used to order the elements of the queue in a specific order. It returns a Null value if the queue follows the default or natural ordering pattern.
Below programs illustrate the java.util.PriorityQueue.comparator() method:
Program 1: When using natural ordering of the elements:
// Java code to illustrate comparator() import java.util.*; public class Priority_Queue_Demo { public static void main(String[] args) { // Creating an empty Priority_Queue PriorityQueue<Integer> queue = new PriorityQueue<Integer>(); // Adding elemetns to the queue queue.add(20); queue.add(24); queue.add(30); queue.add(35); queue.add(45); queue.add(50); System.out.println("Priority queue values are: " + queue); // Creating a comparator Comparator comp = queue.comparator(); // Displaying the comparator values System.out.println("Since the Comparator value is: " + comp); System.out.println("it follows natural ordering"); } } |
Priority queue values are: [20, 24, 30, 35, 45, 50] Since the Comparator value is: null it follows natural ordering
Program 2: When using a specific comparator.
// Java code to illustrate the use of comparator() import java.util.Comparator; import java.util.PriorityQueue; class The_Comparator implements Comparator<String> { public int compare(String str1, String str2) { String first_Str; String second_Str; first_Str = str1; second_Str = str2; return second_Str.compareTo(first_Str); } } public class Priority_Queue_Demo { public static void main(String[] args) { PriorityQueue<String> queue = new PriorityQueue<String>(new The_Comparator()); queue.add("G"); queue.add("E"); queue.add("E"); queue.add("K"); queue.add("S"); queue.add("4"); System.out.println("Queue before using the comparator: " + queue); System.out.println("The elements sorted in descending" + "order:"); for (String element : queue) System.out.print(element + " "); } } |
Queue before using the comparator: [S, K, E, E, G, 4] The elements sorted in descendingorder: S K E E G 4
Recommended Posts:
- Implement PriorityQueue through Comparator in Java
- Stream sorted (Comparator comparator) method in Java
- Java | Collectors minBy(Comparator comparator) with Examples
- Java | Collectors maxBy(Comparator comparator) with Examples
- PriorityBlockingQueue comparator() method in Java
- TreeSet comparator() Method in Java
- PriorityQueue add() Method in Java
- PriorityQueue contains() Method in Java
- Comparator nullsLast() method in Java with examples
- Comparator reversed() method in Java with examples
- Comparator thenComparingLong() method in Java with examples
- Comparator comparingLong() method in Java with examples
- Comparator comparingDouble() method in Java with examples
- Comparator nullsFirst() method in Java with examples
- Comparator thenComparingDouble() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



