Copyque.add(d);
if (que.size() > YOUR_LIMIT)
     que.poll();

or did I missunderstand your question?

edit: forgot to mention that for this to work you probably have to invert your comparTo function since it will throw away the one with highest priority each cycle. (if a is "better" b compare (a, b) should return a positvie number.

example to keep the biggest numbers use something like this:

Copypublic int compare(Double first, Double second) {
            // keep the biggest values
            return first > second ? 1 : -1;
        }
Answer from getekha on Stack Overflow
🌐
Oracle
docs.oracle.com › cd › E17802_01 › j2se › j2se › 1.5.0 › jcp › rc › apidiffs › java › util › PriorityQueue.html
java.util Class PriorityQueue<E>
Creates a PriorityQueue containing the elements in the specified collection. The priority queue has an initial capacity of 110% of the size of the specified collection or 1 if the collection is empty.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › PriorityQueue.html
PriorityQueue (Java Platform SE 8 )
3 weeks ago - A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue, its capacity grows automatically.
🌐
GeeksforGeeks
geeksforgeeks.org › java › priority-queue-in-java
PriorityQueue in Java - GeeksforGeeks
It also supports iteration through Iterable ... This method creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
Published   3 weeks ago
🌐
Guava
guava.dev › releases › 19.0 › api › docs › com › google › common › collect › MinMaxPriorityQueue.html
MinMaxPriorityQueue (Guava: Google Core Libraries for Java 19.0 API)
A double-ended priority queue, which provides constant-time access to both its least element and its greatest element, as determined by the queue's specified comparator. If no comparator is given at creation time, the natural order of elements is used. If no maximum size is given at creation ...
🌐
Medium
medium.com › @greekykhs › all-about-priorityqueue-in-java-d5220dee7feb
A Guide to PriorityQueue in Java. What is PriorityQueue in Java? | by Himaanshu Shukla | Medium
July 6, 2024 - Internally, the PriorityQueue in Java uses an array to store its elements. This array automatically grows in size if the initial capacity (which is 11 by default in JDK 17) is not large enough to hold all the elements added to the queue.
Find elsewhere
🌐
GitHub
gist.github.com › changbinwang › 6051895
Fixed size priority queue · GitHub
Fixed size priority queue · Raw · FixedSizePriorityQueue.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
GitHub
github.com › jimbozhang › fixed-size-priority-queue
GitHub - jimbozhang/fixed-size-priority-queue: A priority queue with fixed size, based on STL. When the maximum size was reached, the element with the lowest priority would be removed automatically. · GitHub
A priority queue with fixed size, based on STL. When the maximum size was reached, the element with the lowest priority would be removed automatically. - jimbozhang/fixed-size-priority-queue
Starred by 5 users
Forked by 2 users
Languages   C++ 98.7% | Makefile 1.3%
🌐
Java2s
java2s.com › example › java › data-structure › implements-a-priority-queue-using-a-max-heap-the-heap-is-of-fixed-siz.html
Implements a priority queue using a max heap. The heap is of fixed size and represented using an array. - Java Data Structure
Implements a priority queue using a max heap. The heap is of fixed size and represented using an array. import java.util.Arrays; public class PriorityQueue { int[] heap;/*from w w w .jav a 2 s .
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › PriorityQueue.html
PriorityQueue (Java Platform SE 7 )
A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue, its capacity grows automatically.
🌐
GeeksforGeeks
geeksforgeeks.org › java › priorityqueue-size-method-in-java
PriorityQueue size() Method in Java - GeeksforGeeks
December 10, 2018 - // Java code to illustrate size() import java.util.*; public class PriorityQueueDemo { public static void main(String args[]) { // Creating an empty PriorityQueue PriorityQueue<String> queue = new PriorityQueue<String>(); // Use add() method to add elements into the Queue queue.add("Welcome"); queue.add("To"); queue.add("Geeks"); queue.add("For"); queue.add("Geeks"); // Displaying the PriorityQueue System.out.println("PriorityQueue: " + queue); // Displaying the size of the PriorityQueue System.out.println("The size of the queue is: " + queue.size()); } }
🌐
Baeldung
baeldung.com › home › java › java collections › fixed size queue implementations in java
Fixed Size Queue Implementations in Java | Baeldung
January 27, 2024 - Finally, we’ll create a fixed-size queue implementation. The Java Collections Framework offers different queue implementations which we can use depending on our needs. For instance, if we need a thread-safe implementation, we could use the ConcurrentLinkedQueue. Likewise, if we need to specify how the elements must be ordered inside the queue, we could use the PriorityQueue...
🌐
Stack Exchange
cs.stackexchange.com › questions › 109823 › when-inserting-an-element-in-a-priority-queue-and-the-heap-size-is-already-at-ma
When inserting an element in a priority queue and the heap size is already at max capacity, should you output an error OR increase the array size? - Computer Science Stack Exchange
May 25, 2019 - In array-based heap implementations I'm familiar with, it is typical to use array resizing where the new size is a constant multiple of the previous size (a.k.a. ArrayList) to avoid quadratic performance on repeated insertions. You can see this in the implementation of the Java standard library's PriorityQueue class, for example. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 10 Would it make sense to use an array of linked lists for a Priority Queue, given a fixed number of priorities?
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.priorityqueue
PriorityQueue Class (Java.Util) | Microsoft Learn
An unbounded priority Queue queue based on a priority heap. [Android.Runtime.Register("java/util/PriorityQueue", DoNotGenerateAcw=true)] [Java.Interop.JavaTypeParameters(new System.String[] { "E" })] public class PriorityQueue : Java.Util.AbstractQueue, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ISerializable
🌐
University of Texas at Dallas
personal.utdallas.edu › ~dheroy › 4331code › book › doc › java › util › PriorityQueue.html
Class java.util.PriorityQueue
The priority queue has an initial capacity of 110% of the size of the specified collection or 1 if the collection is empty. If the specified collection is an instance of a java.util.SortedSet or is another PriorityQueue, the priority queue will be sorted according to the same comparator, or ...
🌐
Coding Shuttle
codingshuttle.com › java-programming-handbook › java-priority-queue
Java PriorityQueue | Coding Shuttle
April 9, 2025 - By default, PriorityQueue in Java functions as a Min Heap, meaning the smallest element is always at the head of the queue.
🌐
Quora
quora.com › Can-we-use-an-ArrayList-for-a-priority-queue-in-Java-If-yes-how-can-it-be-done-correctly
Can we use an ArrayList for a priority queue in Java? If yes, how can it be done correctly? - Quora
There is no built-in priority queue data structure in Java, but one can be implemented using an array. One approach is to keep the array sorted so that the element with the highest priority is always at the front of the queue.