Categories
Uncategorized

Priority Queue Comparator using a HashMap

This is similar to a MinHeap.

import java.util.*; 
import java.lang.*;
 
public class GFG { 

    public static void main(String[] args) 
    { 
        HashMap<Integer, Integer> map 
            = new HashMap<>(); 
            map.put(10, 3);
            map.put(20, 2);
            map.put(30, 1);
      PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a,b) -> map.get(a) - map.get(b));
      pq.add(10);
      pq.add(30);
      pq.add(20);
      System.out.println(pq.poll());
      System.out.println(pq.poll());
      System.out.println(pq.poll());
    }   
}

Output

30
20
10