Queue&Deque

发布时间 2023-11-10 15:28:10作者: anpeiyong

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.

Queue被设计用来 优先处理元素

除Collection提供的基本操作,Queue提供了insert、extract、inspection;

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.

Queue的方法存在2种模式:

依赖具体操作 抛出异常/返回null或false;

 

Summary of Queue methods:

Insert:Queue#add add(e)、Queue#offer offer(e)

Remove:Queue#remove remove()、Queue#poll poll()

Examine:Queue#element element()、Queue#peek 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 <em>head</em> of the queue is that element which would be removed by a call to {@link #remove() } or {@link #poll()}.  

Queue通常(不是必须)FIFO对元素进行排序;

优先级Queue是例外,排序依赖提供的comparator/元素的自然排序,LIFO Queue依赖元素 LIFO;

无论使用什么排序,Queue的head调用remove、poll方法可以被remove;

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.

FIFO Queue,所有新的元素被插入到Queue的tail;

每个Queue的实现 必须指定它的排序属性;

 

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 &quot;bounded&quot;) queues.

offer方法 插入元素,否则返回false;

 

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}.

remove、poll方法 移除并返回Queue的head

 

The {@link #element()} and {@link #peek()} methods return, but do not remove, the head of the queue.

element、peek 返回Queue的head,不移除

 

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中没有定义 block Queue方法;

在BlockingQueue中定义了;

 

{@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的实现 通常不允许null,尽管有些实现如LinkedList没有禁止null;

 

public interface Queue<E> extends Collection<E> {
        boolean offer(E e);
        E remove();
        E poll();
        E element();
        E peek();
    }

  

 

Deque

概述

A linear collection that supports element insertion and removal at both ends.
The name <i>deque</i> is short for "double ended queue" and is usually pronounced "deck".

Most {@code Deque} implementations place no fixed limits on the number of elements they may contain, but this interface supports capacity-restricted deques as well as those with no fixed size limit.

 

Deque支持双端 insert、remove元素

Deque的命名是double ended queue的缩写

大多数Deque的实现 对其包含的元素数量没有限制;

 

This interface defines methods to access the elements at both ends of the deque.
Methods are provided to insert, remove, and examine the element.
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 Deque} implementations; in most implementations, insert operations cannot fail.

Deque定义的方法 支持双端访问元素;

 

Summary of Deque methods:

Insert:Deque#addFirst addFirst(e)、Deque#offerFirst offerFirst(e)、Deque#addLast addLast(e)、Deque#offerLast offerLast(e)
Remove:Deque#removeFirst removeFirst()、Deque#pollFirst pollFirst()、Deque#removeLast removeLast()、Deque#pollLast pollLast()
Examine:Deque#getFirst getFirst()、Deque#peekFirst peekFirst()、Deque#getLast getLast()、Deque#peekLast peekLast()


This interface extends the {@link Queue} interface.
When a deque is used as a queue, FIFO (First-In-First-Out) behavior results.
Elements are added at the end of the deque and removed from the beginning.

Deque扩展了Queue;

当Deque作为一个Queue使用时,FIFO;元素在end被add、在begin被remove;

 

Deques can also be used as LIFO (Last-In-First-Out) stacks.
This interface should be used in preference to the legacy {@link Stack} class.

When a deque is used as a stack, elements are pushed and popped from the beginning of the deque. 

Deque也可以被当做LIFO的stack

应该使用Deque而不是Stack;

当Deque被用作Stack,元素push、pop都从Deque的begin;

 

This interface provides two methods to remove interior elements, {@link #removeFirstOccurrence removeFirstOccurrence} and {@link #removeLastOccurrence removeLastOccurrence}.

Deque提供了2个方法用来remove内部元素:removeFirstOccurrence、removeLastOccurrence;

 

While {@code Deque} implementations are not strictly required to prohibit the insertion of null elements, they are strongly encouraged to do so.
Users of any {@code Deque} implementations that do allow null elements are strongly encouraged <i>not</i> to take advantage of the ability to insert nulls.
This is so because {@code null} is used as a special return value by various methods to indicated that the deque is empty.

Deque的实现不严格禁止null,但强烈建议禁止null

 

public interface Deque<E> extends Queue<E> {
        
    }