Java之Queue接口中add()/offer()、remov

2019-01-17  本文已影响0人  曾泽浩

队列的使用在日常开发中,特别常见,但是对于队列接口中的一些方法可能使用起来有些疑惑,本文简单记录一下关于Queue接口中几种类似方法的区别。

附上源码以及中文注释:

public interface Queue<E> extends Collection<E> {
    /**
     * 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.
     * 插入一个具体的元素到队列中如果没有超过容量限制并且返回true。
     * 如果没有队列已满则抛出IllegalStateException
     *
     * @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
     *         如果插入的对象不对,则会抛出类型转换ClassCastException
     * @throws NullPointerException if the specified element is null and
     *         this queue does not permit null elements
     *         如果插入null,则会抛出空指针异常
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     * 添加元素,如果添加成功则返回true,如果队列是满的,则抛出异常
     */
    boolean add(E e);

    /**
     * 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.
     * 插入一个具体的元素到队列中如果没有超过容量限制并且返回true。
     * 如果没有队列已满则返回false。
     * 当使用一个有容量限制的队列时,建议使用该方法offer(),因为使用add()方法当队列满时会
     * 直接抛出异常,则offer()方法则返回false
     *
     * @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
     * 添加元素,如果添加成功则返回true,如果队列是满的,则返回false
     */
    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();

    /**
     * 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
     * 移除队列头的元素并且返回,如果队列为空则返回null
     */
    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();

    /**
     * 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
     * 返回队列头元素但不移除,如果队列为空,则返回null
     */
    E peek();
}

上一篇 下一篇

猜你喜欢

热点阅读