Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set fixed size to PriorityQueue in Java? [duplicate]

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 ?

like image 532
medo0070 Avatar asked Jun 22 '15 19:06

medo0070


1 Answers

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
like image 59
Amita Avatar answered Nov 14 '22 21:11

Amita