2017-05-26 45 views
-2

我试图执行上MessagePriorityQueue其中队列被定义为优先队列不弹出元件在要求的顺序

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),但根据comparable规则,EMERGENCY应总是出现在BREAKINGNEWS之前。我错过了我的任何实施?当我打电话setUpTimeline()

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

我实现了你的代码,它适合我吗?你可以添加“poll()”的代码并将其打印出来吗? – jkys

+0

是的,它从状态从紧急情况下从顶部打印出来,完全按顺序排列。 – jkys

回答

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 

所以看起来你实现可比是罚款的方式。它必须与您检索队列中元素的方式有关。

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

OP,我抬头一看,你正在使用的filterNewsBuffer()和它的实际返回一个ArrayDeque,这就是为什么你有这个问题。

1

无法重现

更新 打印出发生。我建议feeder.filterNewsBuffer()根本不返回PriorityQueue

+0

这已被标记为“不是答案”。它是。比较接受的答案,表明完全相同的事情。 – EJP