我已经制作了一个prority阻塞队列。插入是基于优先级正确完成的。当我删除队列的头部时,队列的最后一个元素成为队列的头部,程序重复。 假设我在队列中插入了123456789。
First deleted : 1; queue elements :92345678
Second deleted: 9; queue elements :8234567
等等。
我需要实现没有维护的队列属性。
即
首先删除:1个队列元素:23456789
第二次删除:2个队列元素:3456789
package com.block.ui;
import java.util.Iterator;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class PriorityBlockQueSamplActivity extends Activity {
/** Called when the activity is first created. */
EditText et;
EditText et2;
Button b1;
Button b2;
Controller c ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
b1 =(Button)findViewById(R.id.button1);
b2 =(Button)findViewById(R.id.button2);
c= new Controller();
Message m = new Message();
m.mPriority=1;
m.Subject="1";
c.insert(m);
Message n = new Message();
n.mPriority=1;
n.Subject="2";
c.insert(n);
Message p = new Message();
p.mPriority=1;
p.Subject="3";
c.insert(p);
Message q = new Message();
q.mPriority=1;
q.Subject="4";
c.insert(q);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Iterator<Message> iter = c.getQueue().iterator();
while(iter.hasNext()){
Message m =iter.next();
Log.i("MESSAGE", "Element "+m.Subject+" priority :"+m.mPriority);
}
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Message m=c.getMessage();
c.delete(m);
}
});
}
}
还有一个班级
`public class Controller {
private int queueSize=300;
Comparator<Message> comparator = new PriorityComaparator();
PriorityBlockingQueue<Message> qu = new PriorityBlockingQueue<Message>(queueSize,comparator);
public Controller() {
}
public void insert(Message msg) {
boolean status = qu.add(msg);
if (status)
log("Added successfully " + msg.Subject + " priority "
+ msg.mPriority + " size :" + qu.size());
else
log("Failed to add " + msg.Subject + " with priority "
+ msg.mPriority);
}
public void delete(Message msg) {
if (qu.contains(msg)) {
boolean status = qu.remove(msg);
if (status)
log("deleted successfully " + msg.Subject + " priority " + msg.mPriority
+ " size :" + qu.size());
else
log("Failed to delete " + msg.Subject + " with priority "+ msg.mPriority);
}
}
public Message getMessage() {
return qu.peek();
}
public PriorityBlockingQueue<Message> getQueue(){
return qu;
}
void log(String msg) {
Log.i("QUEUE", msg);
}
class PriorityComaparator implements Comparator<Message> {
@Override
public int compare(Message m, Message n) {
int y = ((Message) m).mPriority; // 1 -highest 3-least
int z = ((Message) n).mPriority;
if (y < z) {
return -1;
}
if (y > z) {
return 1;
}
return 0;
}
}
}`
答案 0 :(得分:2)
如果在示例中,所有插入的元素都具有相同的优先级,则检索顺序没有很好地定义。来自PriorityQueue(PriorityBlockingQueue从中导出相同的排序规则):
此队列的头部是指定排序的最小元素。如果多个元素绑定的值最小,则头部是其中一个元素 - 关系会被任意破坏。
(强调补充)
答案 1 :(得分:0)
如果您使用的优先级队列是您的个人实现,那么请尝试堆数据结构。
正如wikipage所说:
堆是一种称为优先级队列的抽象数据类型的最有效实现。
以下是page的链接。
请记住,在Java中你不需要实现它,你可以使用标准PriorityQueue类或PriorityBlockingQueue,如果你需要它在多线程程序中工作。