前言
本篇开始我们来分析Collection的最后一个主要分支——Queue队列。
正文
源码解析
package java.util;
/**
* 设计用于在处理之前保留元素的集合。
* 除了基础的Collection操作,队列提供了额外的插入、提取和检查操作。
* 这些方法中的每个都支持两种形式:
* 1、如果操作失败,抛出一个异常;
* 2、如果操作失败,返回一个特殊值(可能是null或者false),根据具体方法实现不同。
* 插入操作的后一种形式主要针对有容量限制的Queue实现,在大多数实现里,插入操作不能失败。
* A collection designed for holding elements prior to processing.
* Besides basic {@link java.util.Collection Collection} operations,
* queues provide additional insertion, extraction, and inspection
* operations. Each of these methods exists in two forms: one throws
* an exception if the operation fails, the other returns a special
* value (either {@code null} or {@code false}, depending on the
* operation). The latter form of the insert operation is designed
* specifically for use with capacity-restricted {@code Queue}
* implementations; in most implementations, insert operations cannot
* fail.
*
* <table BORDER CELLPADDING=3 CELLSPACING=1>
* <caption>Summary of Queue methods</caption>
* <tr>
* <td></td>
* <td ALIGN=CENTER><em>Throws exception</em></td>
* <td ALIGN=CENTER><em>Returns special value</em></td>
* </tr>
* <tr>
* <td><b>Insert</b></td>
* <td>{@link Queue#add add(e)}</td>
* <td>{@link Queue#offer offer(e)}</td>
* </tr>
* <tr>
* <td><b>Remove</b></td>
* <td>{@link Queue#remove remove()}</td>
* <td>{@link Queue#poll poll()}</td>
* </tr>
* <tr>
* <td><b>Examine</b></td>
* <td>{@link Queue#element element()}</td>
* <td>{@link Queue#peek peek()}</td>
* </tr>
* </table>
*
* 通常来说,队列的元素是按照FIFO(先进先出)的顺序排序的,但是也不是一定的。
* 优先队列和LIFO的队列(或堆栈stacks)属于例外情况。
* 优先队列的元素排序是根据提供的比较器,或者元素的自然排序来排序。
* LIFO的队列则是根据元素的LIFO来排序(后进先出)。
*
* 无论用了哪种排序方式,队列的头元素一定是通过调用remove或poll方法返回的那个元素。
* 在一个FIFO的队列里,所有的新元素都是被插入到tail尾部。其他种类的队列可能使用
* 不同的存放规则。
* 每种Queue的实现都必须明确指定它的排序属性。
* <p>Queues typically, but do not necessarily, order elements in a
* FIFO (first-in-first-out) manner. Among the exceptions are
* priority queues, which order elements according to a supplied
* comparator, or the elements' natural ordering, and LIFO queues (or
* stacks) which order the elements LIFO (last-in-first-out).
* Whatever the ordering used, the <em>head</em> of the queue is that
* element which would be removed by a call to {@link #remove() } or
* {@link #poll()}. In a FIFO queue, all new elements are inserted at
* the <em>tail</em> of the queue. Other kinds of queues may use
* different placement rules. Every {@code Queue} implementation
* must specify its ordering properties.
*
* offer方法插入元素如果失败了,会返回false。而Collection的add方法,在增加失败的时候,
* 只会抛出一个未检查的异常。offer方法主要是为了当操作失败的情况是很普遍的情况下。
* 比如,对于固定容量的或是有界限的队列。
* <p>The {@link #offer offer} method inserts an element if possible,
* otherwise returning {@code false}. This differs from the {@link
* java.util.Collection#add Collection.add} method, which can fail to
* add an element only by throwing an unchecked exception. The
* {@code offer} method is designed for use when failure is a normal,
* rather than exceptional occurrence, for example, in fixed-capacity
* (or "bounded") queues.
*
* remove和poll方法会移除并返回队列的头元素。
* 至于具体哪个元素将被移除,要看各实现类的具体实现方式。
* remove方法和poll方法在队列为空时的表现不同:remove方法会抛出异常,而poll方法会返回null。
* <p>The {@link #remove()} and {@link #poll()} methods remove and
* return the head of the queue.
* Exactly which element is removed from the queue is a
* function of the queue's ordering policy, which differs from
* implementation to implementation. The {@code remove()} and
* {@code poll()} methods differ only in their behavior when the
* queue is empty: the {@code remove()} method throws an exception,
* while the {@code poll()} method returns {@code null}.
*
* element和peek方法返回队列头元素,但不会移除。
* <p>The {@link #element()} and {@link #peek()} methods return, but do
* not remove, the head of the queue.
*
* Queue接口没有定义阻塞队列方法 ,这在并发编程中是常见的。
* 这些等待元素出现或空格变得可用的方法在BlockingQueue接口中定义,该接口扩展了此接口。
* <p>The {@code Queue} interface does not define the <i>blocking queue
* methods</i>, which are common in concurrent programming. These methods,
* which wait for elements to appear or for space to become available, are
* defined in the {@link java.util.concurrent.BlockingQueue} interface, which
* extends this interface.
*
* Queue实现类通常不允许插入null元素,但一些实现,比如LinkedList不禁止插入null元素。
* 即使在允许插入null的队列中,也不应该插入null元素,因为null已经作为poll方法返回的一个
* 表示队列是否包含元素的特殊值。
* <p>{@code Queue} implementations generally do not allow insertion
* of {@code null} elements, although some implementations, such as
* {@link LinkedList}, do not prohibit insertion of {@code null}.
* Even in the implementations that permit it, {@code null} should
* not be inserted into a {@code Queue}, as {@code null} is also
* used as a special return value by the {@code poll} method to
* indicate that the queue contains no elements.
*
* Queue的实现类通常不会定义基于元素的equals和hashcode方法,而是从类别Object继承基于标识的版本,
* 因为基于元素的等式并不总是很好地定义了具有相同元素但不同的排序属性的队列。
* <p>{@code Queue} implementations generally do not define
* element-based versions of methods {@code equals} and
* {@code hashCode} but instead inherit the identity based versions
* from class {@code Object}, because element-based equality is not
* always well-defined for queues with the same elements but different
* ordering properties.
*
*
* <p>This interface is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
* @see java.util.Collection
* @see LinkedList
* @see PriorityQueue
* @see java.util.concurrent.LinkedBlockingQueue
* @see java.util.concurrent.BlockingQueue
* @see java.util.concurrent.ArrayBlockingQueue
* @see java.util.concurrent.LinkedBlockingQueue
* @see java.util.concurrent.PriorityBlockingQueue
* @since 1.5
* @author Doug Lea
* @param <E> the type of elements held in this collection
*/
public interface Queue<E> extends Collection<E> {
/**
* 将指定元素插入到队列中,如果队列容量没有严格限制,且此时有空余容量,则返回true,
* 如果当前没有多余空间,则抛出IllegalStateException异常。
* Inserts the specified element into this queue if it is possible to do so
* immediately without violating capacity restrictions, returning
* {@code true} upon success and throwing an {@code IllegalStateException}
* if no space is currently available.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Collection#add})
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean add(E e);
/**
* 在不违反队列的容量限制的情况下,立刻将指定元素插入到当前队列中。
* 当使用一个容量限制的队列,该方法通常比add方法更好,因为add方法在插入失败的时候只能
* 抛出一个异常。
* Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions.
* When using a capacity-restricted queue, this method is generally
* preferable to {@link #add}, which can fail to insert an element only
* by throwing an exception.
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
boolean offer(E e);
/**
* 检索并移除队列的头结点元素。
* 如果队列为空,该方法会抛出异常。
* Retrieves and removes the head of this queue. This method differs
* from {@link #poll poll} only in that it throws an exception if this
* queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
E remove();
/**
* 检索并移除队列的有结点元素。
* 如果队列为空,则会返回null。
* Retrieves and removes the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
E poll();
/**
* 返回头结点的元素。
* 如果队列为空,会抛出异常。
* Retrieves, but does not remove, the head of this queue. This method
* differs from {@link #peek peek} only in that it throws an exception
* if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
E element();
/**
* 返回头结点的元素。
* 如果队列为空,返回null。
* Retrieves, but does not remove, the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
E peek();
}