本文介绍了具有固定大小的Java PriorityQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算一个algortihm的大量可能的结果组合。为了对这些组合进行排序,我使用double值对它们进行评级,并将它们存储在PriorityQueue中。目前,该队列中有大约20万个项目,这几乎是内存的积极性。实际上,我只需要说出列表中所有项目中最好的1000或100。
所以我开始问自己是否有办法在Java中拥有一个固定大小的优先级队列。我的行为应该是这样的:
项目是否优于已经存储的项目?如果是,请将其插入相应的位置并抛出评级最低的元素。

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

推荐答案

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

还是我错过了解你的问题?

or did I missunderstand your question?

编辑:忘了提到为了这个工作,你可能必须反转你的comparTo函数,因为它会丢弃每个周期具有最高优先级的那个。 (如果a是更好b比较(a,b)应该返回一个正数。

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;
        }

这篇关于具有固定大小的Java PriorityQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:35