我有一个NotificationEvent实例。每当创建此实例时,我都会在队列中添加此实例。队列的名称应为NotificationQueue。
NotificationEvent的结构是这样的:
public class NotificationEvent {
private String sender;
private String receiver;
private String message;
/**
* @return the sender
*/
public String getSender() {
return sender;
}
/**
* @param sender the sender to set
*/
public void setSender(String sender) {
this.sender = sender;
}
/**
* @return the receiver
*/
public String getReceiver() {
return receiver;
}
/**
* @param receiver the receiver to set
*/
public void setReceiver(String receiver) {
this.receiver = receiver;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
NotificationQueue的所需结构应该是什么?
答案 0 :(得分:0)
我建议不要再重新发明轮子。已经在Java运行时库中的接口Queue
定义了队列应具有的操作。这是brief tutorial for the Queue interface和Queue JavaDoc。好吧,这里还有一个example of using Queue implementations。
您可以像这样创建通知队列对象:
Queue<NotificationEvent> eventQueue = new LinkedList<NotificationEvent>;
或者,如果您坚持为队列拥有自己的类型:
public class extends LinkedList<NotificationEvent> {
/**
* Constructs an empty list.
*/
public NotificationQueue() {
}
/**
* Constructs a list containing the elements of the specified collection,
* in the order they are returned by the
* collection's iterator.
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public NotificationQueue(Collection<? extends NotificationEvent> c) {
super(c);
}
}
...
NotificationQueue eventQueue == new NotificationQueue();
注意:
LinkedList
不是Queue接口的唯一可用实现,请参阅Java运行时库中已有的其他实现的Queue JavaDoc。当然,您也可以编写自己的Queue接口实现。