Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java PriorityQueue with fixed size

I am calculating a large number of possible resulting combinations of an algortihm. To sort this combinations I rate them with a double value und store them in PriorityQueue. Currently, there are about 200k items in that queue which is pretty much memory intesive. Acutally, I only need lets say the best 1000 or 100 of all items in the list. So I just started to ask myself if there is a way to have a priority queue with a fixed size in Java. I should behave like this: Is the item better than one of the allready stored? If yes, insert it to the according position and throw the element with the least rating away.

Does anyone have an idea? Thanks very much again!

Marco

like image 220
Marco Avatar asked Dec 04 '09 10:12

Marco


People also ask

Is size of queue fixed?

It is a queue, and the size of the queue is fixed, it means that the queue cannot hold more than specified limit of number of data. In the previous picture the size of the queue is 5 because it is holding 5 integers.

Is Java PriorityQueue Min or Max?

Q #4) Is Java Priority queue max or min? Answer: By default, the priority queue in Java is min Priority queue with natural ordering. To make it max, we have to use a custom comparator so that head of the queue returns the greatest element in the queue.

How is priority queue size defined?

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.

Is a PriorityQueue always sorted?

This priority queue will be sorted according to the same comparator as the given collection, or according to its elements' natural order if the collection is sorted according to its elements' natural order.


4 Answers

que.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:

public int compare(Double first, Double second) {             // keep the biggest values             return first > second ? 1 : -1;         } 
like image 124
getekha Avatar answered Sep 28 '22 12:09

getekha


MinMaxPriorityQueue, Google Guava

There is indeed a class for maintaining a queue that, when adding an item that would exceed the maximum size of the collection, compares the items to find an item to delete and thereby create room: MinMaxPriorityQueue found in Google Guava as of version 8.

EvictingQueue

By the way, if you merely want deleting the oldest element without doing any comparison of the objects’ values, Google Guava 15 gained the EvictingQueue class.

like image 23
Basil Bourque Avatar answered Sep 28 '22 14:09

Basil Bourque


There is a fixed size priority queue in Apache Lucene: http://lucene.apache.org/java/2_4_1/api/org/apache/lucene/util/PriorityQueue.html

It has excellent performance based on my tests.

like image 24
Vladimir Giverts Avatar answered Sep 28 '22 12:09

Vladimir Giverts


Use SortedSet:

SortedSet<Item> items = new TreeSet<Item>(new Comparator<Item>(...));
...
void addItem(Item newItem) {
    if (items.size() > 100) {
         Item lowest = items.first();
         if (newItem.greaterThan(lowest)) {
             items.remove(lowest);
         }
    }

    items.add(newItem);   
}
like image 36
Victor Sorokin Avatar answered Sep 28 '22 14:09

Victor Sorokin