Java PriorityQueue with fixed size

后端 未结 7 1072
梦毁少年i
梦毁少年i 2020-12-02 18:38

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

7条回答
  •  Happy的楠姐
    2020-12-02 18:50

    It seems natural to just keep the top 1000 each time you add an item, but the PriorityQueue doesn't offer anything to achieve that gracefully. Maybe you can, instead of using a PriorityQueue, do something like this in a method:

    List list = new ArrayList();
    ...
    list.add(newOutput);
    Collections.sort(list);
    list = list.subList(0, 1000);
    

提交回复
热议问题