优先级队列不按要求的顺序弹出元素

时间:2017-05-26 00:26:40

标签: java queue

我尝试在PriorityQueue上实现Message,其中队列定义为

Queue<Message> buffer = new PriorityQueue<>();

Message类结构如下,我在Status

上排序
public class Message implements Comparable<Message> {

    public final double timeStamp;
    public final String text;
    public final Topic topic;
    public final Status status;

    public Message(double timeStamp, String text, Topic topic, Status status) {
        this.timeStamp = timeStamp;
        this.text = text;
        this.topic = topic;
        this.status = status;
    }

    public enum Status {
        EMERGENCY(10),
        BREAKING_NEWS(5),
        NORMAL(3),
        SPECIAL_INTEREST(1);// ,UNKNOWN(0);

        private final int value;

        Status(int i) {
            value = i;
        }

        public int value() {
            return value;
        }
    }

    @Override 
    public int compareTo(Message o) {
        return o.status.value() - this.status.value();
    }
}

执行buffer.poll()的结果:

Message{timeStamp=26.97700461385856, text='Large asteroid to hurtle past Earth on April 19', topic=SCITECH, status=EMERGENCY}
Message{timeStamp=84.12641199950579, text='Searches for 'World War 3' hit their highest level this month since records started in 2004', topic=MISCELLANEOUS, status=BREAKING_NEWS}
Message{timeStamp=46.66933694702732, text='Potato chip prices spike in Japan due to panic buying', topic=MISCELLANEOUS, status=EMERGENCY}
Message{timeStamp=29.120910393303486, text='N. Korea to strike US bases in Asian Pacific', topic=INTERNATIONAL, status=NORMAL}
Message{timeStamp=19.29303724679007, text='China says North Korea tension has to be stopped from reaching 'irreversible' stage', topic=INTERNATIONAL, status=NORMAL}
Message{timeStamp=7.783619682487895, text='North Korea says it 'will go to war' if US provokes it', topic=INTERNATIONAL, status=NORMAL}
Message{timeStamp=54.18702698620533, text='Space Station expedition 50 crew touches down in Kazakhstan after 170 days in orbit', topic=SCITECH, status=BREAKING_NEWS}
Message{timeStamp=61.94491799102393, text='Sydney teen dies after being shot in bedroom', topic=DOMESTIC, status=NORMAL}
Message{timeStamp=66.82604459580855, text='Russia boycotts Eurovision after contestant barred from entering Ukraine', topic=MISCELLANEOUS, status=NORMAL}
Message{timeStamp=75.62219739785463, text='The Reserve Bank expresses concern that a third of Australian borrowers have little to no buffer on their home loan repayments', topic=BUSINESS, status=NORMAL}
Message{timeStamp=38.310966295396824, text='Bake bread not war: Russian military helps Syrians restore Aleppo bakery', topic=INTERNATIONAL, status=NORMAL}
Message{timeStamp=43.16996080256394, text='Tillerson backs down on ultimatum mission to Russia', topic=INTERNATIONAL, status=NORMAL}
Message{timeStamp=23.906919960845325, text='Huge US bomb kills dozens of IS militants in Afghanistan', topic=INTERNATIONAL, status=NORMAL}
Message{timeStamp=51.402727352370064, text='London's police failing to cope with soaring gun & knife violence', topic=MISCELLANEOUS, status=NORMAL}
Message{timeStamp=113.39969152008928, text='DNC Head Tom Perez Speaks While American Flag Falls In Background', topic=MISCELLANEOUS, status=BREAKING_NEWS}
Message{timeStamp=59.15406825059532, text='In pot we trust: International Church of Cannabis to open in Colorado', topic=MISCELLANEOUS, status=SPECIAL_INTEREST}
Message{timeStamp=15.624370796970345, text='Iranian President Rouhani to seek second term, faces hardline challenge', topic=INTERNATIONAL, status=SPECIAL_INTEREST}
Message{timeStamp=32.470579156848764, text='Top secret CIA virus control system: WikiLeaks releases Hive', topic=SCITECH, status=SPECIAL_INTEREST}
Message{timeStamp=70.88195473641044, text='Australia's unemployment rate remains steady at 5.9 per cent in March', topic=BUSINESS, status=NORMAL}
Message{timeStamp=34.632544119734426, text='Forget the 'Mother of all bombs', meet the Russian-made 'Daddy'', topic=INTERNATIONAL, status=SPECIAL_INTEREST}
Message{timeStamp=79.46017180938442, text='Christian leaders urge hope amid disaster and conflict', topic=MISCELLANEOUS, status=SPECIAL_INTEREST}
Message{timeStamp=4.111351533465954, text='Satirist John Clarke, of Clarke and Dawe fame, dies aged 68', topic=DOMESTIC, status=SPECIAL_INTEREST}
Message{timeStamp=88.72527895011933, text='N. Korean missile fails, blows up 'almost immediately' after launch - Seoul & US military', topic=INTERNATIONAL, status=NORMAL}
Message{timeStamp=91.01962002800698, text='Blast hits bus convoy near Aleppo, women & children among dozens killed. Holland blames Assad', topic=INTERNATIONAL, status=NORMAL}
Message{timeStamp=93.83338178031829, text='Trump supporters, opponents clash in California park', topic=MISCELLANEOUS, status=SPECIAL_INTEREST}
Message{timeStamp=97.5813554620491, text='Turks vote in historic referendum on expanding Erdogan's power', topic=INTERNATIONAL, status=SPECIAL_INTEREST}
Message{timeStamp=101.40498006185564, text='Uber's revenue hits $6.5 billion in 2016, still has large loss', topic=BUSINESS, status=NORMAL}
Message{timeStamp=103.9840260269641, text='Apple receives permit in California to test self-driving cars', topic=BUSINESS, status=SPECIAL_INTEREST}
Message{timeStamp=108.71954836698345, text='Drug-addicted python rehabilitated by Australian prisoners', topic=MISCELLANEOUS, status=SPECIAL_INTEREST}
Message{timeStamp=11.30134270959088, text='Assad dismisses Syrian gas attack as '100 per cent fabrication'', topic=INTERNATIONAL, status=NORMAL}

结果与预期不符,例如,第二次打印的状态为BREAKINGNEW(5),第三次打印的状态为EMERGENCY(10),但EMERGENCY应始终显示在BREAKINGNEWS之前根据{{​​1}}规则。我错过了实施的任何内容吗?

更新 当我拨打comparable

时,就会打印出来
setUpTimeline()

2 个答案:

答案 0 :(得分:1)

无法重现。我建议feeder.filterNewsBuffer()根本不会返回PriorityQueue

答案 1 :(得分:0)

我复制并粘贴了你的消息类并用几个值测试它,我得到了正确的输出。这是我做的:

Queue<Message> buffer = new PriorityQueue<>();
    buffer.add(new Message(Status.NORMAL));
    buffer.add(new Message(Status.EMERGENCY));
    buffer.add(new Message(Status.SPECIAL_INTEREST));
    buffer.add(new Message(Status.BREAKING_NEWS));
    while (!buffer.isEmpty()) {
        System.out.println(buffer.poll().status.value());
    }

此代码的输出为

10
5
3
1

所以看起来你实现Comparable的方式很好。它必须与您检索队列中元素的方式有关。

编辑:对不起,值得注意的是,我从Message构造函数中删除了几乎所有参数,因为它们与测试无关。

OP,我查看了您正在使用的filterNewsBuffer(),它实际上正在返回ArrayDeque,这就是您遇到此问题的原因。