public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
HashMap<String, PriorityQueue<Long>> hmap = new HashMap<String, PriorityQueue<Long>>();
for (long i = 0; i < n; ++i) {
String s = sc.next();
long m = sc.nextLong();
if(hmap.containsKey(s))
hmap.get(s).add(m);
else {
PriorityQueue<Long> maxHeap = new PriorityQueue<Long>(Collections.reverseOrder());
maxHeap.add(m);
hmap.put(s, maxHeap);
}
}
for (String str : hmap.keySet()) {
System.out.println(str);
System.out.println(hmap.get(str));
}
}
Hi - I am learning how to use data structures in Java. Here, I am trying to use the PriorityQueue as a Max-Heap. But when I am printing the PriorityQueue associated with each String in the Hashmap - the numbers are not in the descending order as I would have expected. Could someone help me understand why ?