看到 LinkedList 的时候,发现继承了 Deque,而 Deque 又继承了 Queue,所以转来看看队列的顶层接口 Queue

类注释:

①:A collection designed for holding elements prior(优先) to processing. Besides basic 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 null or false, depending on the operation). ③:The latter form of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail.

Queue 是被设计用来处理保存的元素的集合,除了基本的 Collection 接口中定义的操作, Queue 还额外提供了 插入(insertion)、获取(extraction)、检查(inspection) 操作。

:这些方法都有两种形式:一种失败时抛出异常,一种返回一个特定的值(具体返回 null 还是 返回布尔值 false 根据不同的实现而定)

:当插入元素失败时,返回一个特定的值是专门为了有上限的队列【也就是有界队列】而设计的,大多数情况下,insert 操作并不会失败。

image-20201016004331868

方法失败时抛出异常失败时返回特定值
Insertadd(e)offer(e)
Removeremove()poll()
Examineelement()peek()

①: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 head of the queue is that element which would be removed by a call to remove() or poll(). ③:In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.

①:Queue 通常但并不一定以 FIFO 先进先出队列的形式对元素进行排序。

②:非 FIFO 的例外情况包括:

  • 优先级队列(priority queues),它会根据提供的比较器(comparator)对元素进行排序,或保持元素的自然顺序。
  • LIFO 后进先出 队列。

无论使用哪种顺序,队列的开头都是该元素,可以调用 remove()poll() 进行删除。【这里的 that element 指的是哪个元素呢?不太理解】

③:在 FIFO 队列中,新插入的元素总是在队尾,其他队列则可能采用不同的放置规则。每个队列都必须实现自己的排序规则。

The offer method inserts an element if possible, otherwise returning false. This differs from the Collection.add method, which can fail to add an element only by throwing an unchecked exception. The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues.

offer 方法的作用是在可能的情况下插入一个元素,否则返回 false,这与 Collection 接口中的 add 方法不同, add 在添加元素失败时会抛出一个运行时异常。

offer 方法的为插入元素失败是正常情况的场景设计,例如在一个有界队列中使用。

The remove() and 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 remove() and poll() methods differ(使...不同) only in their behavior when the queue is empty: the remove() method throws an exception, while the poll() method returns null.

removepoll 方法的作用都是删除队列头部的元素,并将其作为方法的返回值。区别仅仅是当队列为空时, remove() 会抛出异常,而 poll() 则只会返回 null。

The element() and peek() methods return, but do not remove, the head of the queue.
The Queue interface does not define the blocking queue methods, which are common in concurrent programming. These methods, which wait for elements to appear or for space to become available, are defined in the java.util.concurrent.BlockingQueue interface, which extends this interface.

element()peek() 方法都返回但是并不删除队列的首个元素。 Queue 并没有定义在并发环境中使用的阻塞队列 blocking queue 的方法,这些等待元素出现或队列内有空间可使用的方法在 java.util.concurrent.BlockingQueue 接口中定义, BlockingQueue 扩展了 Queue 接口。

①:Queue implementations generally do not allow insertion of null elements, although some implementations, such as LinkedList, do not prohibit insertion of null. Even in the implementations that permit it, null should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.

队列 Queue 的实现一般不允许 null 元素存在,比如 LinkedList不允许插入 null 元素,即使在允许 null 元素存在的队列中,也不应该将 null 插入到队列中,因为 poll 方法可能会将 null 作为特殊值进行返回,如果队列中本身就存在 null 元素,则会引起歧义。

②:Queue implementations generally do not define element-based versions of methods equals and hashCode but instead inherit the identity based versions from class Object, because element-based equality is not always well-defined for queues with the same elements but different ordering properties.

This interface is a member of the Java Collections Framework.

队列实现通常并不定义 equals 和 hashCode 基于元素 的实现,而是从 Object 类中继承基于 identity 的实现,因为在队列中,具有相同元素但顺序属性不同的队列,基于元素的 equal 定义并不合适。【因为对 Queue 并不是很了解,且还没看源码,所以这一段 eqauls 的定义翻译不准确,看完代码之后进行修正】

类结构

image-20201016014814173

可以看到 Queue 中定义的方法非常简单,就是2套存取元素的函数。

总结:

Queue 作为存储元素的容器,可以定义多种元素的存储方式,并且提供2套对元素进行操作的方法,一种在失败时抛出异常,一种则将失败情况当成正常情况进行处理,可以应对更多的场景。

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

最是人间留不住,曾是惊鸿照影来。