Guava MinMaxPriorityQueue
Starting to write Java code for research, I wanted a better Priority Queue than the default Java one. I was told to try MinMaxPriorityQueue
from Guava.
The next statements create a new MinMaxPriorityQueue
with k
places:
public static MinMaxPriorityQueue<Double> pq2;
pq2 = MinMaxPriorityQueue.maximumSize(k).create();
Do not forget to import com.google.common.collect.MinMaxPriorityQueue;
.
As expected, elements are added to the Priority Queue using the add()
method (based on the existing values). Last, we can print the first and last elements in the queue, without removing them from the queue, as follows:
System.out.println("First: " + pq2.peekFirst() + " Last: " + pq2.peekLast());
You can find more about MinMaxPriorityQueue
here.