VOOZH about

URL: https://www.geeksforgeeks.org/java/priorityblockingqueue-comparator-method-in-java/

⇱ PriorityBlockingQueue comparator() method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

PriorityBlockingQueue comparator() method in Java

Last Updated : 11 Jul, 2025

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. 

Output:
Comparator value: null
PriorityBlockingQueuefollows natural ordering

Example 2: To demonstrate comparator() method on PriorityBlockingQueue which contains a list of integers. 

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