Change priorityQueue to max priorityqueue

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

I have priority queue in Java of Integers:

 PriorityQueue pq= new PriorityQueue();

When I call pq.poll(

17条回答
  •  鱼传尺愫
    2020-12-04 06:12

    Change PriorityQueue to MAX PriorityQueue Method 1 : Queue pq = new PriorityQueue<>(Collections.reverseOrder()); Method 2 : Queue pq1 = new PriorityQueue<>((a, b) -> b - a); Let's look at few Examples:

    public class Example1 {
        public static void main(String[] args) {
    
            List ints = Arrays.asList(222, 555, 666, 333, 111, 888, 777, 444);
            Queue pq = new PriorityQueue<>(Collections.reverseOrder());
            pq.addAll(ints);
            System.out.println("Priority Queue => " + pq);
            System.out.println("Max element in the list => " + pq.peek());
            System.out.println("......................");
            // another way
            Queue pq1 = new PriorityQueue<>((a, b) -> b - a);
            pq1.addAll(ints);
            System.out.println("Priority Queue => " + pq1);
            System.out.println("Max element in the list => " + pq1.peek());
            /* OUTPUT
              Priority Queue => [888, 444, 777, 333, 111, 555, 666, 222]
              Max element in the list => 888
              ......................
               Priority Queue => [888, 444, 777, 333, 111, 555, 666, 222]
               Max element in the list => 888
    
             */
    
    
        }
    }
    

    Let's take a famous interview Problem : Kth Largest Element in an Array using PriorityQueue

    public class KthLargestElement_1{
        public static void main(String[] args) {
    
            List ints = Arrays.asList(222, 555, 666, 333, 111, 888, 777, 444);
            int k = 3;
            Queue pq = new PriorityQueue<>(Collections.reverseOrder());
            pq.addAll(ints);
            System.out.println("Priority Queue => " + pq);
            System.out.println("Max element in the list => " + pq.peek());
            while (--k > 0) {
                pq.poll();
            } // while
            System.out.println("Third largest => " + pq.peek());
    /*
     Priority Queue => [888, 444, 777, 333, 111, 555, 666, 222]
    Max element in the list => 888
    Third largest => 666
    
     */
        }
    }
    

    Another way :

    public class KthLargestElement_2 {
        public static void main(String[] args) {
            List ints = Arrays.asList(222, 555, 666, 333, 111, 888, 777, 444);
            int k = 3;
    
            Queue pq1 = new PriorityQueue<>((a, b) -> b - a);
            pq1.addAll(ints);
            System.out.println("Priority Queue => " + pq1);
            System.out.println("Max element in the list => " + pq1.peek());
            while (--k > 0) {
                pq1.poll();
            } // while
            System.out.println("Third largest => " + pq1.peek());
            /*
              Priority Queue => [888, 444, 777, 333, 111, 555, 666, 222] 
              Max element in the list => 888 
              Third largest => 666
    
             */
        }
    }
    

    As we can see, both are giving the same result.

提交回复
热议问题