Change priorityQueue to max priorityqueue

后端 未结 17 1827
一整个雨季
一整个雨季 2020-12-04 05:13

I have priority queue in Java of Integers:

 PriorityQueue pq= new PriorityQueue();

When I call pq.poll(

17条回答
  •  萌比男神i
    2020-12-04 06:18

    In Java 8+ you can create a max priority queue via one of these methods:

    Method 1:

    PriorityQueue maxPQ = new PriorityQueue<>(Collections.reverseOrder()); 
    

    Method 2:

    PriorityQueue maxPQ = new PriorityQueue<>((a,b) -> b - a); 
    

    Method 3:

    PriorityQueue maxPQ = new PriorityQueue<>((a,b) -> b.compareTo(a)); 
    

提交回复
热议问题