Java Collections Java Java API
java.util.PriorityQueue
public Comparator<? super E> comparator()
Returns the comparator used to order the elements in this queue, or null if this queue is sorted according to the natural ordering of its elements.
null
package com.logicbig.example.priorityqueue;import java.util.*;public class ComparatorExample { public static void main(String... args) { PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder()); Comparator<? super Integer> comparator = pq.comparator(); List<Integer> list = new ArrayList<>(List.of(5, 3, 4, 2, 1)); Collections.sort(list, comparator); System.out.println(list); }}
[5, 4, 3, 2, 1]