Queue接口
All Superinterfaces:
All Known Subinterfaces:
BlockingDeque,BlockingQueue,Deque,TransferQueue
All Known Implementing Classes:
常用类:LinkedList,PriorityQueue
队列接口用于在处理数据之前暂存数,队列接口定义了6种方法。
队列的操作都是围绕着队列的头部与尾部节点进行。
每一个队列方法都存在两种形式,如下图所示:
队列通常是FIFO顺序。无论队列顺序是什么,remove()与poll()都是移除队列的头部节点。队列的顺序决定了新插入节点的插入位置。
队列接口实现通常需要限制容量即有界
java.util中的实现是无界队列,java.util.concurrent的实现是有界队列。
队列的实现通常不允许NULL值的插入
因为poll()与peek()通过返回NULL来表明队列为空。但是队列接口的一个实现-LinkedList违背了这一点。尽管如此,在使用LinkedList作为队列的过程中,也要避免插入NULL。
AbstractQueue
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
public E element() {
E x = peek();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
骨架队列类实现了add,element,remove这几个操作。这几个操作是对实现类实现的offer,peek,poll的封装。骨架队列类还实现了 clear与addAll。