Java 优先级队列未按所需顺序弹出元素

Java 优先级队列未按所需顺序弹出元素,java,queue,Java,Queue,我尝试在Message上实现PriorityQueue,其中队列定义为 Queue<Message> buffer = new PriorityQueue<>(); 执行buffer.poll()的结果 结果与预期不符,例如,第二次打印的状态为BREAKINGNEW(5),第三次打印的状态为EMERGENCY(10),但根据可比的规则,EMERGENCY应始终出现在BREAKINGNEWS之前。我是否错过了我的实现 更新 当我调用setUpTimeline() priv

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

Queue<Message> buffer = new PriorityQueue<>();
执行buffer.poll()的结果

结果与预期不符,例如,第二次打印的状态为
BREAKINGNEW(5)
,第三次打印的状态为
EMERGENCY(10)
,但根据
可比的
规则,
EMERGENCY
应始终出现在
BREAKINGNEWS
之前。我是否错过了我的实现

更新 当我调用
setUpTimeline()

private void setUpTimeline(){
双偏移量=displayWidth+20;//启动消息流的x-pos
双播放时间=0;
Queue messageBuffer=feeder.filterNewsBuffer(filteredTopics);
而(!messageBuffer.isEmpty()){
Map colorMap=setColorMap();
时间线=新时间线();
最新消息;
//印刷品
System.out.println(messageBuffer.peek());
latestMessage=messageBuffer.poll();
Message.Status Status=latestMessage.Status;
Message.Topic Topic=latestMessage.Topic;
字符串messageBody=latestMessage.text++\uD83D\uDE40;
Text Text=新文本(偏移量,显示高度-15,消息体);
text.setFont(Font.Font(“Tahoma”,fontwweight.BLACK,80));
text.setTextOrigin(VPos.BASELINE);
setFill(colorMap.get(topic.toString());
double mesWidth=text.getLayoutBounds().getWidth();
播放时间+=平均宽度/播放速度;
为运行设置新闻片段(文本、组、时间线、播放时间);
偏移量+=宽度;
}
}

无法复制。我建议
feeder.filterNewsBuffer()
根本不返回
PriorityQueue

我复制并粘贴了您的消息类,并使用几个值对其进行了测试,得到了正确的输出。以下是我所做的:

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());
    }
所以看起来你的实现方式很好。这一定与您检索队列中元素的方式有关

编辑:很抱歉,值得注意的是,我几乎从
消息
构造函数中删除了所有参数,因为它们与测试无关


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

我实现了您的代码,它对我来说运行正常吗?你能添加你在其中投票的代码并打印出来吗?是的,它是按状态从顶部的紧急状态向下打印出来的,完全按顺序。这被标记为“不是答案”。它是。比较说明完全相同事情的公认答案。
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}
    private void setUpTimeline() {
        double offset = displayWidth + 20; // starting x-pos for message stream
        double playtime = 0;
        Queue<Message> messageBuffer = feeder.filterNewsBuffer(filteredTopics);

        while (!messageBuffer.isEmpty()) {
            Map<String,Color> colorMap = setColorMap();
            Timeline timeline = new Timeline();
            Message latestMessage;

            //THE PRINT
            System.out.println(messageBuffer.peek());

            latestMessage = messageBuffer.poll();
            Message.Status status = latestMessage.status;
            Message.Topic topic = latestMessage.topic;
            String messageBody = latestMessage.text + " " + "\uD83D\uDE40";
            Text text = new Text(offset, displayHeight - 15, messageBody);
            text.setFont(Font.font("Tahoma", FontWeight.BLACK, 80));
            text.setTextOrigin(VPos.BASELINE);
            text.setFill(colorMap.get(topic.toString()));
            double mesWidth = text.getLayoutBounds().getWidth();
            playtime += mesWidth / playSpeed;
            setNewsPieceForRun(text, group, timeline, playtime);
            offset += mesWidth;
        }
    }
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