min max heap java

Solutions on MaxInterview for min max heap java by the best coders in the world

showing results for - "min max heap java"
Nabil
27 Oct 2018
1import java.util.PriorityQueue;
2
3public class MaxHeapWithPriorityQueue {
4
5    public static void main(String args[]) {
6        // create priority queue
7        PriorityQueue<Integer> prq = new PriorityQueue<>(Comparator.reverseOrder());
8
9        // insert values in the queue
10        prq.add(6);
11        prq.add(9);
12        prq.add(5);
13        prq.add(64);
14        prq.add(6);
15
16        //print values
17        while (!prq.isEmpty()) {
18            System.out.print(prq.poll()+" ");
19        }
20    }
21
22}
Nicolás
27 Feb 2016
1// min heap: PriorityQueue implementation from the JDK
2PriorityQueue<Integer> prq = new PriorityQueue<>();
3
4// max heap: PriorityQueue implementation WITH CUSTOM COMPARATOR PASSED
5// Method 1: Using Collections (recommended)
6PriorityQueue<Integer> prq = new PriorityQueue<>(Collections.reverseOrder());
7// Method 2: Using Lambda function (may cause Integer Overflow)
8PriorityQueue<Integer> prq = new PriorityQueue<>((a, b) -> b - a);