Java PriorityQueue with fixed size

后端 未结 7 1086
梦毁少年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条回答
  •  囚心锁ツ
    2020-12-02 18:51

    Use SortedSet:

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

提交回复
热议问题