PriorityQueue<StoreEmail> emails = new PriorityQueue<StoreEmail> (n,
new Comparator<StoreEmail> () {
public int compare(StoreEmail a, StoreEmail b) {
if(a.urgency != b.urgency){
return b.urgency - a.urgency;
}
else{
return a.timestamp - b.timestamp;
}
}
}
);
public class StoreEmail
{
String emailContent;
int urgency;
long timestamp;
StoreEmail(String emailContent, int urgency,long timestamp){
this.emailContent = emailContent;
this.urgency = urgency;
this.timestamp = timestamp;
}
}
插入队列
StoreEmail storeEmail = new StoreEmail(in.next(),in.nextInt(),System.currentTimeMillis());
emails.add(storeEmail);
对于上面的比较器,在优先级队列中插入以下值。
store email value
store email5 4
store email4 4
store email3 4
store email2 4
store email1 4
它在每次运行中给出不同的结果,这意味着比较器不能正常工作,并且无法根据时间戳进行排序。
注意:希望根据电子邮件值维护FIFO顺序进行排序。 有人可以帮我解决这个问题。 提前致谢。已经浪费了很多时间。