如何在 Java 中为 PriorityQueue 设置固定大小?

How to set fixed size to PriorityQueue in Java?

我有一个简单的问题。我正在我的项目中实施 PriorityQueue。我的问题是我可以为 PriorityQueue 设置一个固定大小是 Java 吗?

正如此处的 oracle 文档所述, http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html:

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. The details of the growth policy are not specified.

一个可能的解决方法是您可以在执行任何操作之前检查大小:

if (q.size() <= QUEUE_LIMIT)
     //your code