PriorityBlockingQueue comparator() method in Java Last Updated : 03 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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); } } Output:Comparator value: null PriorityBlockingQueuefollows natural ordering Example 2: To demonstrate comparator() method on PriorityBlockingQueue which contains a list of integers. Java // 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 + ", "); } } Output:Comparator value is: COMPARING@28d93b30 PriorityBlockingQueue follows: COMPARING@28d93b30 The elements after custom Comparator forGeeks, Geeks, A computer portal, Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#comparator-- Comment More infoAdvertise with us Next Article PriorityBlockingQueue comparator() method in Java A AmanSingh2210 Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-PriorityBlockingQueue +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads 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 a null value if the queue follows the natural ordering pattern of the elements.Syntax: comp_set = (Priori 2 min read PriorityBlockingQueue clear() method in Java The clear() method of PriorityBlockingQueue removes all the elements from this queue. Therefore this method can be applied when it is required to clear the PriorityBlockingQueue. Syntax: public void clear() Parameter: This method takes no parameters. Returns: This method returns nothing. Exception: 2 min read PriorityBlockingQueue add() Method in Java The add(E e) method of PriorityBlockingQueue inserts the element passed as a parameter to the method at the tail of this PriorityBlockingQueue. This method returns true if the adding of the element is successful. Else it returns false. Syntax: public boolean add(E e) Parameter: This method takes a m 2 min read PriorityBlockingQueue contains() method in Java The contains(Object o) method checks whether PriorityBlockingQueue contains an object o or not. This method returns true, if and only if, this queue contains at least one element e which is equal to object o passed as parameter i.e. e.equals(o). If queue does not contains the Object o, then method r 2 min read PriorityBlockingQueue iterator() method in Java The iterator() method of PriorityBlockingQueue class Returns an iterator over the elements in this queue. The elements returned from this method do not follow any order. The returned iterator is weakly consistent. Syntax: public Iterator iterator() Parameter: This method does not take any parameter. 2 min read Like