How to set fixed size to PriorityQueue in Java?

11k views Asked by At

I have a simple question. I am implementing PriorityQueue in my project. My question is can i set a fixed size for PriorityQueue is Java ?

1

There are 1 answers

0
Amita On BEST ANSWER

As the oracle documentation here states, 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.

A possible workaround may be that you can check the size before performing any operation:

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