VI Prerequisites: Comparable in PQ for generic PQ
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));
NOTE: Implementation ()s of priority Queue is totally same as heap DS function which is covered in heap DS.
- add() = minInsert()
- peek() = getMin()
- remove() = extractMin()