Change priorityQueue to max priorityqueue

后端 未结 17 1795
一整个雨季
一整个雨季 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:14

    You can provide a custom Comparator object that ranks elements in the reverse order:

    PriorityQueue<Integer> pq = new PriorityQueue<Integer>(defaultSize, new Comparator<Integer>() {
        public int compare(Integer lhs, Integer rhs) {
            if (lhs < rhs) return +1;
            if (lhs.equals(rhs)) return 0;
            return -1;
        }
    });
    

    Now, the priority queue will reverse all its comparisons, so you will get the maximum element rather than the minimum element.

    Hope this helps!

    0 讨论(0)
  • 2020-12-04 06:14

    The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time.

    The Comparator should override the compare method.

    int compare(T o1, T o2)
    

    Default compare method returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

    The Default PriorityQueue provided by Java is Min-Heap, If you want a max heap following is the code

    public class Sample {
        public static void main(String[] args) {
            PriorityQueue<Integer> q = new PriorityQueue<Integer>(new Comparator<Integer>() {
    
                public int compare(Integer lhs, Integer rhs) {
                    if(lhs<rhs) return +1;
                    if(lhs>rhs) return -1;
                    return 0;
                }
            });
            q.add(13);
            q.add(4);q.add(14);q.add(-4);q.add(1);
            while (!q.isEmpty()) {
                System.out.println(q.poll());
            }
        }
    
    }
    

    Reference :https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html#comparator()

    0 讨论(0)
  • 2020-12-04 06:15

    I just ran a Monte-Carlo simulation on both comparators on double heap sort min max and they both came to the same result:

    These are the max comparators I have used:

    (A) Collections built-in comparator

     PriorityQueue<Integer> heapLow = new PriorityQueue<Integer>(Collections.reverseOrder());
    

    (B) Custom comparator

    PriorityQueue<Integer> heapLow = new PriorityQueue<Integer>(new Comparator<Integer>() {
        int compare(Integer lhs, Integer rhs) {
            if (rhs > lhs) return +1;
            if (rhs < lhs) return -1;
            return 0;
        }
    });
    
    0 讨论(0)
  • 2020-12-04 06:18

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

    Method 1:

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

    Method 2:

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

    Method 3:

    PriorityQueue<Integer> maxPQ = new PriorityQueue<>((a,b) -> b.compareTo(a)); 
    
    0 讨论(0)
  • 2020-12-04 06:19

    We can do this by creating our CustomComparator class that implements Comparator interface and overriding its compare method. Below is the code for the same :

    import java.util.PriorityQueue;
    import java.util.Comparator;
    public class Main
    {
        public static void main(String[] args) {
            PriorityQueue<Integer> nums = new PriorityQueue<>(new CustomComparator());
            nums.offer(21);
            nums.offer(1);
            nums.offer(8);
            nums.offer(2);
            nums.offer(-4);
            System.out.println(nums.peek());
        }
    }
    class CustomComparator implements Comparator<Integer>{
        @Override
        public int compare(Integer n1, Integer n2){
            int val = n1.compareTo(n2);
            if(val > 0)
               return -1;
            else if(val < 0)
                return 1;
            else
                return 0;
        }
    }
    
    0 讨论(0)
提交回复
热议问题