I want to get the sorted key value pair on value, and if the values are equal, I wanna sort on keys asc.
I tried using PriorityQueue like following but I am getting null pointer for some reason. Please help me understand the issue and probably answer my query.
This is what I came up with, but unfortunately the output of the code is
PQ : [Hello, Yes]
Where as I was expecting
PQ : [No, Hello]
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>() {{
put("Hello", 50);
put("Yes", 50);
put("No", 100);
}};
PriorityQueue<String> pq = new PriorityQueue<>((a, b) -> {
if (map.get(a).equals(map.get(b))) return a.compareTo(b);
else return Integer.compare(map.get(b), map.get(a));
});
verifyPQ(map, pq);
System.out.println(pq);
}
private static void verifyPQ(Map<String, Integer> map, PriorityQueue<String> pq) {
for (String key : map.keySet()) {
pq.add(key);
if (pq.size() > 2) {
pq.poll();
}
}
}
compare(map.get(b), map.get(a))means that100comes before50so the"No"entry will be removed first; trycompare(map.get(a), map.get(b))PriorityQueueonly guarantees that the first element (head) ist sorted ]