Java 集合系列(12): Deque接口源码

package java.util;

/**
 *双向队列 
 *队头:可以插入可以删除 
 *队尾:可以插入可以删除 
 *继承Queue接口
 *
 * @author Doug Lea
 * @author Josh Bloch
 * @since  1.6
 * @param <E> the type of elements held in this collection
 */
public interface Deque<E> extends Queue<E> {
    /**
     * 插入一个元素到队列前面
     *
     * @param 插入一个元素到队列前面
     * @throws IllegalStateException 如果超出了容量限制就抛出异常
     * @throws ClassCastException 如果指定元素的类阻止其加入队列抛出异常
     * @throws NullPointerException 如果添加的元素为null,而该实现类不允许null加入,则抛出异常
     * @throws IllegalArgumentException 如果一些其他的限制某些类型的数据加入其中,则抛出异常
     */
    void addFirst(E e);

    /**
     * 插入一个元素到队列后面
     *
     * @param 插入一个元素到队列后面
     * @throws IllegalStateException 如果超出了容量限制就抛出异常
     * @throws ClassCastException 如果指定元素的类阻止其加入队列抛出异常
     * @throws NullPointerException 如果添加的元素为null,而该实现类不允许null加入,则抛出异常
     * @throws IllegalArgumentException 如果一些其他的限制某些类型的数据加入其中,则抛出异常
     */
    void addLast(E e);

    /**
     * 插入一个元素到队列前面,如果违反容量限制,就抛出异常
     * @param 插入一个元素到队列前面
     * @return 插入一个元素到队列前面,插入成功,还回true;插入失败,还回false
     * @throws ClassCastException 如果指定元素的类阻止其加入队列抛出异常
     * @throws NullPointerException 如果添加的元素为null,而该实现类不允许null加入,则抛出异常
     * @throws IllegalArgumentException 如果一些其他的限制某些类型的数据加入其中,则抛出异常
     */
    boolean offerFirst(E e);

    /**
     * 插入一个元素到队列后面,如果违反容量限制,就抛出异常
     * @param 插入一个元素到队列后面
     * @return 插入一个元素到队列后面,插入成功,还回true;插入失败,还回false
     * @throws ClassCastException 如果指定元素的类阻止其加入队列抛出异常
     * @throws NullPointerException 如果添加的元素为null,而该实现类不允许null加入,则抛出异常
     * @throws IllegalArgumentException 如果一些其他的限制某些类型的数据加入其中,则抛出异常
     */
    boolean offerLast(E e);

    /**
     *检索并删除此deque的第一个元素。 
     *
     * @return 还回第一个元素
     * @throws NoSuchElementException 如果队列是空
     */
    E removeFirst();

    /**
     *检索并删除此deque的最后一个元素。 
     *
     * @return 还回最后一个元素
     * @throws NoSuchElementException 如果队列是空
     */
    E removeLast();

    /**
     *检索并删除此deque的第一个元素。 
     *
     * @return 还回第一个元素,如果队列是空则还回null值
     */
    E pollFirst();

    /**
     *检索并删除此deque的最后一个元素。 
     *
     * @return 还回最后一个元素,如果队列是空则还回null值
     */
    E pollLast();

    /**
     * 检索并获得此deque的第一个元素。
     * @return 还回第一个元素
     * @throws NoSuchElementException 如果是空的队列就抛异常
     */
    E getFirst();

    /**
     * 检索并获得此deque的最后一个元素。
     * @return 还回最后一个元素
     * @throws NoSuchElementException 如果是空的队列就抛异常
     */
    E getLast();

    /**
     * 检索并获得此deque的第一个元素。
     * @return 返回第一个元素, 或者如果是空的队列则返回null
     */
    E peekFirst();

    /**
     * 检索并获得此deque的最后一个元素。
     * @return 返回最后一个元素, 或者如果是空的队列则返回null
     */
    E peekLast();

    /**
     * 从此deque中删除指定元素的第一个出现。 
     *
     * @return 从此deque中删除指定元素的第一个出现,成功就返回true
     * @throws ClassCastException 如果指定元素的类与此队列不相容
     * @throws NullPointerException 如果该元素为null且不允许为null
     */
    boolean removeFirstOccurrence(Object o);

    /**
     * 从此deque中删除指定元素的最后一个出现。 
     *
     * @return 从此deque中删除指定元素的最后一个出现,成功就返回true
     * @throws ClassCastException 如果指定元素的类与此队列不相容
     * @throws NullPointerException 如果该元素为null且不允许为null
     */
    boolean removeLastOccurrence(Object o);

    // Queue methods (队列的方法)

    /**
     * Inserts the specified element into the queue represented by this deque
     * (in other words, at the tail of this deque) 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.
     * When using a capacity-restricted deque, it is generally preferable to
     * use {@link #offer(Object) offer}.
     *
     * <p>This method is equivalent to {@link #addLast}.
     *
     * @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 deque
     * @throws NullPointerException if the specified element is null and this
     *         deque does not permit null elements
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this deque
     */
    boolean add(E e);

    /**
     * Inserts the specified element into the queue represented by this deque
     * (in other words, at the tail of this deque) if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * {@code true} upon success and {@code false} if no space is currently
     * available.  When using a capacity-restricted deque, this method is
     * generally preferable to the {@link #add} method, which can fail to
     * insert an element only by throwing an exception.
     *
     * <p>This method is equivalent to {@link #offerLast}.
     *
     * @param e the element to add
     * @return {@code true} if the element was added to this deque, else
     *         {@code false}
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this deque
     * @throws NullPointerException if the specified element is null and this
     *         deque does not permit null elements
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this deque
     */
    boolean offer(E e);

    /**
     * Retrieves and removes the head of the queue represented by this deque
     * (in other words, the first element of this deque).
     * This method differs from {@link #poll poll} only in that it throws an
     * exception if this deque is empty.
     *
     * <p>This method is equivalent to {@link #removeFirst()}.
     *
     * @return the head of the queue represented by this deque
     * @throws NoSuchElementException if this deque is empty
     */
    E remove();

    /**
     * Retrieves and removes the head of the queue represented by this deque
     * (in other words, the first element of this deque), or returns
     * {@code null} if this deque is empty.
     *
     * <p>This method is equivalent to {@link #pollFirst()}.
     *
     * @return the first element of this deque, or {@code null} if
     *         this deque is empty
     */
    E poll();

    /**
     * Retrieves, but does not remove, the head of the queue represented by
     * this deque (in other words, the first element of this deque).
     * This method differs from {@link #peek peek} only in that it throws an
     * exception if this deque is empty.
     *
     * <p>This method is equivalent to {@link #getFirst()}.
     *
     * @return the head of the queue represented by this deque
     * @throws NoSuchElementException if this deque is empty
     */
    E element();

    /**
     * Retrieves, but does not remove, the head of the queue represented by
     * this deque (in other words, the first element of this deque), or
     * returns {@code null} if this deque is empty.
     *
     * <p>This method is equivalent to {@link #peekFirst()}.
     *
     * @return the head of the queue represented by this deque, or
     *         {@code null} if this deque is empty
     */
    E peek();


    // *** Stack methods ***

    /**
     * Pushes an element onto the stack represented by this deque (in other
     * words, at the head of this deque) if it is possible to do so
     * immediately without violating capacity restrictions, throwing an
     * {@code IllegalStateException} if no space is currently available.
     *
     * <p>This method is equivalent to {@link #addFirst}.
     *
     * @param e the element to push
     * @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 deque
     * @throws NullPointerException if the specified element is null and this
     *         deque does not permit null elements
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this deque
     */
    void push(E e);

    /**
     * Pops an element from the stack represented by this deque.  In other
     * words, removes and returns the first element of this deque.
     *
     * <p>This method is equivalent to {@link #removeFirst()}.
     *
     * @return the element at the front of this deque (which is the top
     *         of the stack represented by this deque)
     * @throws NoSuchElementException if this deque is empty
     */
    E pop();


    // *** Collection methods ***

    /**
     * Removes the first occurrence of the specified element from this deque.
     * If the deque does not contain the element, it is unchanged.
     * More formally, removes the first element {@code e} such that
     * <tt>(o==null ? e==null : o.equals(e))</tt>
     * (if such an element exists).
     * Returns {@code true} if this deque contained the specified element
     * (or equivalently, if this deque changed as a result of the call).
     *
     * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
     *
     * @param o element to be removed from this deque, if present
     * @return {@code true} if an element was removed as a result of this call
     * @throws ClassCastException if the class of the specified element
     *         is incompatible with this deque
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if the specified element is null and this
     *         deque does not permit null elements
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     */
    boolean remove(Object o);

    /**
     * Returns {@code true} if this deque contains the specified element.
     * More formally, returns {@code true} if and only if this deque contains
     * at least one element {@code e} such that
     * <tt>(o==null ? e==null : o.equals(e))</tt>.
     *
     * @param o element whose presence in this deque is to be tested
     * @return {@code true} if this deque contains the specified element
     * @throws ClassCastException if the type of the specified element
     *         is incompatible with this deque
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if the specified element is null and this
     *         deque does not permit null elements
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     */
    boolean contains(Object o);

    /**
     * Returns the number of elements in this deque.
     *
     * @return the number of elements in this deque
     */
    public int size();

    /**
     * Returns an iterator over the elements in this deque in proper sequence.
     * The elements will be returned in order from first (head) to last (tail).
     *
     * @return an iterator over the elements in this deque in proper sequence
     */
    Iterator<E> iterator();

    /**
     * Returns an iterator over the elements in this deque in reverse
     * sequential order.  The elements will be returned in order from
     * last (tail) to first (head).
     *
     * @return an iterator over the elements in this deque in reverse
     * sequence
     */
    Iterator<E> descendingIterator();

}

猜你喜欢

转载自blog.csdn.net/oqkdws/article/details/79982456