The comparator() method of PriorityBlockingQueue returns the comparator that can be used to order the elements in a PriorityBlockingQueue. The method returns null value if the queue follows the natural ordering pattern of the elements.
Syntax:
public Comparator<? super E> comparator()
Returns: This method returns the comparator set used to order the elements of the set in a specific order. It returns a null value if the PriorityBlockingQueue follows the default or natural ordering pattern.
Below programs illustrate comparator() method of PriorityBlockingQueue:
Example 1: To demonstrate comparator() method on PriorityBlockingQueue which contains a list of integers.
// Java Program Demonstrate comparator() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; import java.util.*; public class GFG { public static void main(String[] args) throws InterruptedException { // create object of PriorityBlockingQueue PriorityBlockingQueue<Integer> PrioQueue = new PriorityBlockingQueue<Integer>(); // Add numbers to PriorityBlockingQueue PrioQueue.put( 45815616 ); PrioQueue.put( 4981561 ); PrioQueue.put( 4594591 ); PrioQueue.put( 9459156 ); // get String representation of PriorityBlockingQueue String str = PrioQueue.toString(); // Creating a comparator using comparator() Comparator comp = PrioQueue.comparator(); // Displaying the comparator values System.out.println( "Comparator value: " + comp); if (comp == null ) System.out.println( "PriorityBlockingQueue" + "follows natural ordering" ); else System.out.println( "PriorityBlockingQueue follows" + comp); } } |
Comparator value: null PriorityBlockingQueuefollows natural ordering
Example 2: To demonstrate toString() method on PriorityBlockingQueue which contains list of Strings.
// Java Program Demonstrate comparator() // method of PriorityBlockingQueue import java.util.concurrent.PriorityBlockingQueue; import java.util.*; // Comparator to compare Strings class COMPARING implements Comparator<String> { public int compare(String str1, String str2) { return str2.compareTo(str1); } } public class GFG { public static void main(String[] args) throws InterruptedException { // define capacity of PriorityBlockingQueue int capacityOfQueue = 5 ; // create object of PriorityBlockingQueue // by passing capacity and comparator class // as parameters. PriorityBlockingQueue<String> characters = new PriorityBlockingQueue<String>(capacityOfQueue, new COMPARING()); // Add Strings characters.add( "Geeks" ); characters.add( "forGeeks" ); characters.add( "A computer portal" ); // Getting the comparator using comparator() Comparator comp = characters.comparator(); // Displaying the comparator values System.out.println( "Comparator value is: " + comp); if (comp == null ) System.out.println( "PriorityBlockingQueue" + "follows natural ordering" ); else System.out.println( "PriorityBlockingQueue follows: " + comp); // display result System.out.println( "\nThe elements after custom Comparator" ); for (String e : characters) System.out.print(e + ", " ); } } |
Comparator value is: COMPARING@28d93b30 PriorityBlockingQueue follows: COMPARING@28d93b30 The elements after custom Comparator forGeeks, Geeks, A computer portal,
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.