[JAVA] Priority Queue max Make it a queue

I forgot a little, so I'll make a note here.

Usually the Priority Queue is the minimum heap.

PriorityQueue pq= new PriorityQueue(); pq.poll (); // This gets the smallest element

To be able to get this in order from the maximum heap

PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder());

You can do it.

Later, if you want to use a specific field of the object PriorityQueue pq = new PriorityQueue(Comparator.comparing(SumTree::getAge)); Or PriorityQueue pq = new PriorityQueue(Comparator.comparing(SumTree::getAge).reversed());

You can do it.

Recommended Posts

Priority Queue max Make it a queue
[Java] Make it a constant
Run Scala with GraalVM & make it a native image
Make a reflection utility ②
Make a reflection utility ③
Make a reflection utility ①
[Summary] Why make it object-oriented?
[Rails] Make a breadcrumb trail
Make a rhombus using Java