public interface Queue
implements Collection<E>
| java.util.Queue<E> |
|
AbstractQueue<E>,
ArrayBlockingQueue<E>,
ArrayDeque<E>,
BlockingDeque<E>,
BlockingQueue<E>,
ConcurrentLinkedDeque<E>,
ConcurrentLinkedQueue<E>,
DelayQueue<E extends
Delayed>,
Deque<E>,
LinkedBlockingDeque<E>,
LinkedBlockingQueue<E>,
LinkedList<E>,
LinkedTransferQueue<E>,
PriorityBlockingQueue<E>,
PriorityQueue<E>,
SynchronousQueue<E>,
TransferQueue<E>
|
专为处理前保存元素而设计的收集器。 除了基本的Collection操作外,队列还提供额外的插入,提取和检查操作。 每种方法都以两种形式存在:一种操作失败时抛出异常,另一种返回特殊值(根据操作而定)( null或false )。 插入操作的后一种形式专门用于容量限制的Queue实现; 在大多数实现中,插入操作不会失败。
| Throws exception | Returns special value | |
| Insert | add(e) |
offer(e) |
| Remove | remove() |
poll() |
| Examine | element() |
peek() |
队列通常但不一定以FIFO(先进先出)方式排列元素。 例外的是优先级队列,它根据提供的比较器对元素进行排序,或者元素的自然顺序,以及对元素LIFO(后进先出)进行排序的LIFO队列(或堆栈)。 无论使用何种排序,队列头都是通过调用remove()或poll()将被删除的元素。 在FIFO队列中,所有新元素都插入队列尾部 。 其他种类的队列可能会使用不同的放置规则。 每个Queue实现都必须指定其排序属性。
如果可能的话, offer方法插入一个元素,否则返回false 。 这与Collection.add方法不同,后者可能无法仅通过抛出未经检查的异常来添加元素。 offer方法设计用于故障是正常情况,而不是例外情况,例如,在固定容量(或“有界”)队列中。
remove()和poll()方法删除并返回队列的头部。 从队列中删除哪个元素是队列的排序策略的一个函数,该策略因实现而异。 remove()和poll()方法仅在队列为空时的行为不同: remove()方法抛出异常,而poll()方法返回null 。
element()和 peek()方法返回,但不删除队列的头部。
Queue接口没有定义阻塞队列方法 ,这在并发编程中很常见。 这些等待元素出现或空间可用的方法在BlockingQueue接口中定义,该接口扩展了此接口。
Queue实现通常不允许插入null元素,但某些实现(如LinkedList )不禁止插入null 。 即使在允许的实现中, null也不应插入到Queue ,因为null也被poll方法用作特殊返回值,以指示该队列不包含任何元素。
Queue实现通常不定义方法 equals和 hashCode的基于元素的版本,而是继承基类 Object基于版本的版本,因为基于元素的相等性并不总是为具有相同元素但具有不同排序属性的队列定义良好。
公共方法(Public methods) |
|
|---|---|
abstract boolean |
add(E e) 将指定的元素插入此队列中,如果它是立即可行且不会违反容量限制,返回 |
abstract E |
element() 检索但不删除此队列的头部。 |
abstract boolean |
offer(E e) 如果可以立即执行而不违反容量限制,则将指定的元素插入此队列中。 |
abstract E |
peek() 检索但不移除此队列的头部,或者如果此队列为空,则返回 |
abstract E |
poll() 检索并删除此队列的头部,或者如果此队列为空,则返回 |
abstract E |
remove() 检索并删除此队列的头部。 |
继承方法(Inherited methods) |
|
|---|---|
java.util.Collection
|
|
java.lang.Iterable
|
|
boolean add (E e)
将指定的元素插入此队列中,如果它是立即可行且不会违反容量限制,返回 true在成功和抛出 IllegalStateException ,如果当前没有空间可用。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 返回(Returns) | |
|---|---|
boolean |
true (as specified by add(E)) |
| 抛出异常(Throws) | |
|---|---|
IllegalStateException |
if the element cannot be added at this time due to capacity restrictions |
ClassCastException |
if the class of the specified element prevents it from being added to this queue |
NullPointerException |
if the specified element is null and this queue does not permit null elements |
IllegalArgumentException |
if some property of this element prevents it from being added to this queue |
E element ()
检索但不删除此队列的头部。 此方法与peek仅在于,如果此队列为空,则会引发异常。
| 返回(Returns) | |
|---|---|
E |
the head of this queue |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
if this queue is empty |
boolean offer (E e)
如果可以立即执行而不违反容量限制,则将指定的元素插入此队列中。 在使用容量受限的队列时,通常优选此方法为add(E) ,该方法无法仅通过引发异常来插入元素。
| 参数(Parameters) | |
|---|---|
e |
E: the element to add |
| 返回(Returns) | |
|---|---|
boolean |
true if the element was added to this queue, else false |
| 抛出异常(Throws) | |
|---|---|
ClassCastException |
if the class of the specified element prevents it from being added to this queue |
NullPointerException |
if the specified element is null and this queue does not permit null elements |
IllegalArgumentException |
if some property of this element prevents it from being added to this queue |
E peek ()
检索但不移除此队列的头部,或者如果此队列为空,则返回 null 。
| 返回(Returns) | |
|---|---|
E |
the head of this queue, or null if this queue is empty |
E poll ()
检索并删除此队列的头部,或者如果此队列为空,则返回 null 。
| 返回(Returns) | |
|---|---|
E |
the head of this queue, or null if this queue is empty |
E remove ()
检索并删除此队列的头部。 此方法与poll仅在于,如果此队列为空,则会引发异常。
| 返回(Returns) | |
|---|---|
E |
the head of this queue |
| 抛出异常(Throws) | |
|---|---|
NoSuchElementException |
if this queue is empty |